Skip to content

Commit

Permalink
fast-fix(auth): add test user & token to remove auto logging in
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderrudnik committed Feb 7, 2022
1 parent 623097a commit 512028f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
18 changes: 3 additions & 15 deletions src/components/router/RequireAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import React, { useEffect } from 'react';
import { Navigate, useNavigate } from 'react-router-dom';
import { useAppDispatch, useAppSelector } from '@app/hooks/reduxHooks';
import { doLogin } from '@app/store/slices/authSlice';
import { initValues } from '../auth/LoginForm/LoginForm';
import React from 'react';
import { Navigate } from 'react-router-dom';
import { useAppSelector } from '@app/hooks/reduxHooks';

const RequireAuth: React.FC = ({ children }) => {
const dispatch = useAppDispatch();
const token = useAppSelector((state) => state.auth.token);
const navigate = useNavigate();

useEffect(() => {
if (!token) {
dispatch(doLogin(initValues))
.unwrap()
.then(() => navigate('/'));
}
}, [token, dispatch, navigate]);

return token ? <>{children}</> : <Navigate to="/auth/login" replace />;
};
Expand Down
36 changes: 33 additions & 3 deletions src/services/localStorage.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
import { UserModel } from '@app/domain/UserModel';
import avatarImg from '@app/assets/avatars/avatar5.png';

const testUser = {
id: 1,
firstName: 'Christopher',
lastName: 'Johnson',
imgUrl: avatarImg,
userName: '@john1989',
email: {
name: '[email protected]',
verified: true,
},
phone: {
number: '+18143519459',
verified: true,
},
sex: 'male',
birthday: '01/26/2022',
lang: 'en',
country: 'GB',
city: 'London',
address1: '14 London Road',
zipcode: 5211,
website: 'altence.com',
socials: {
twitter: '@altence_team',
facebook: 'https://facebook.com/groups/1076577369582221',
linkedin: 'https://linkedin.com/company/altence',
},
};

export const persistToken = (token: string): void => {
localStorage.setItem('accessToken', token);
};

export const readToken = (): string | null => {
return localStorage.getItem('accessToken');
export const readToken = (): string => {
return localStorage.getItem('accessToken') || 'bearerToken';
};

export const persistUser = (user: UserModel): void => {
Expand All @@ -15,7 +45,7 @@ export const persistUser = (user: UserModel): void => {
export const readUser = (): UserModel | null => {
const userStr = localStorage.getItem('user');

return userStr ? JSON.parse(userStr) : null;
return userStr ? JSON.parse(userStr) : testUser;
};

export const deleteToken = (): void => localStorage.removeItem('accessToken');
Expand Down

0 comments on commit 512028f

Please sign in to comment.