Guest User

Untitled

a guest
Sep 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. export const Auth = {
  2. isAuthenticated: false,
  3. authenticate() {
  4. this.isAuthenticated = true;
  5. },
  6. admin() {
  7. this.admin = jwtDecode(Token.jwt).admin;
  8. },
  9. logout() {
  10. this.isAuthenticated = false;
  11. },
  12. };
  13.  
  14. export const checkAuth = () => {
  15. if (cookies.get('jwt') == null) return false;
  16. const jwt = jwtDecode(cookies.get('jwt').jwt);
  17.  
  18. const currentTime = Date.now() / 1000;
  19.  
  20. if (jwt.exp < currentTime) {
  21. return false;
  22. }
  23.  
  24. return Auth.authenticate();
  25. };
  26.  
  27. export const AUTH_REQUEST = (username, password) => {
  28. const url = `${getBaseUrl()}/m/v1/auth`;
  29.  
  30. const data = {
  31. username,
  32. password,
  33. };
  34.  
  35. return axios.post(url, data).then((response) => {
  36. cookies.set('jwt', response.data, { path: '/' });
  37. Auth.authenticate();
  38. });
  39. };
  40.  
  41. AUTH_REQUEST.propTypes = {
  42. username: PropTypes.string.isRequired,
  43. password: PropTypes.string.isRequired,
  44. };
Add Comment
Please, Sign In to add comment