Skip to content

Commit

Permalink
feat: set login with id/pw
Browse files Browse the repository at this point in the history
  • Loading branch information
healim01 committed Jan 24, 2024
1 parent aba9f8c commit 2568d87
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
11 changes: 0 additions & 11 deletions src/api/userApi.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import axiosInstance, { endpoints } from 'src/utils/axios';

// TODO : Add type
// export const userLogin = async (data: any) => {
// const response = axiosInstance.post(endpoints.auth.login, data);
// return response;
// };

export const GetFirstInfo = async () => {
const response = axiosInstance.get(endpoints.auth.info);
return response;
};

// export const GetUser = async () => {
// const response = await axios.get(endpoints.user.list);
// return response;
// };
6 changes: 2 additions & 4 deletions src/auth/context/jwt/auth-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ export function AuthProvider({ children }: Props) {
}, [initialize]);

// LOGIN
// const login = useCallback(async (email: string, password: string) => {
const login = useCallback(async (name: string, email: string) => {
const login = useCallback(async (email: string, password: string) => {
const data = {
name,
email,
// password,
password,
};

const res = await axios.post(`${BASE_URL}${endpoints.auth.login}`, data);
Expand Down
2 changes: 1 addition & 1 deletion src/auth/context/jwt/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const tokenExpired = (exp: number) => {

// Test token expires after 10s
// const timeLeft = currentTime + 10000 - currentTime; // ~10s
const timeLeft = exp * 1000 - currentTime;
const timeLeft = exp * 10000 - currentTime;

clearTimeout(expiredTimer);

Expand Down
6 changes: 2 additions & 4 deletions src/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export type AuthStateType = {
// ----------------------------------------------------------------------

type CanRemove = {
login?: (name: string, email: string) => Promise<void>;
// login?: (email: string, password: string) => Promise<void>;
login?: (email: string, password: string) => Promise<void>;
register?: (
email: string,
password: string,
Expand All @@ -52,8 +51,7 @@ export type JWTContextType = CanRemove & {
loading: boolean;
authenticated: boolean;
unauthenticated: boolean;
login: (name: string, email: string) => Promise<void>;
// login: (email: string, password: string) => Promise<void>;
login: (email: string, password: string) => Promise<void>;
register: (email: string, password: string, firstName: string, lastName: string) => Promise<void>;
logout: () => Promise<void>;
};
Expand Down
41 changes: 28 additions & 13 deletions src/sections/auth/jwt/jwt-login-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import Alert from '@mui/material/Alert';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
// routes
import { useRouter } from 'src/routes/hooks';
import { useSearchParams, useRouter } from 'src/routes/hooks';
// config
import { PATH_AFTER_LOGIN } from 'src/config-global';
// auth
import { useAuthContext } from 'src/auth/hooks';
// components
import FormProvider, { RHFTextField } from 'src/components/hook-form';
import { IconButton, InputAdornment } from '@mui/material';
import { RouterLink } from 'src/routes/components';
import { paths } from 'src/routes/paths';
import Iconify from 'src/components/iconify';
import { useBoolean } from 'src/hooks/use-boolean';

// ----------------------------------------------------------------------

Expand All @@ -24,16 +29,22 @@ export default function JwtLoginView() {

const router = useRouter();

const searchParams = useSearchParams();

const returnTo = searchParams.get('returnTo');

const password = useBoolean();

const [errorMsg, setErrorMsg] = useState('');

const LoginSchema = Yup.object().shape({
name: Yup.string().required('name is required'),
email: Yup.string().required('Email is required').email('Email must be a valid email address'),
password: Yup.string().required('Password is required'),
});

const defaultValues = {
name: '최혜림',
email: '[email protected]',
password: '1234',
};

const methods = useForm({
Expand All @@ -49,9 +60,9 @@ export default function JwtLoginView() {

const onSubmit = handleSubmit(async (data) => {
try {
await login?.(data.name, data.email);
await login?.(data.email, data.password);

router.push(PATH_AFTER_LOGIN);
router.push(returnTo || PATH_AFTER_LOGIN);
} catch (error) {
console.error(error);
reset();
Expand All @@ -61,18 +72,26 @@ export default function JwtLoginView() {

const renderHead = (
<Stack spacing={2} sx={{ mb: 5 }}>
<Typography variant="h4">Sign in to Hanspace</Typography>
<Typography variant="h4">Hanspace 입장하기</Typography>

<Stack direction="row" spacing={0.5}>
<Typography variant="body2">회원이 아니세요?</Typography>

<Link component={RouterLink} href={paths.auth.jwt.register} variant="subtitle2">
회원가입
</Link>
</Stack>
</Stack>
);

const renderForm = (
<Stack spacing={2.5}>
{!!errorMsg && <Alert severity="error">{errorMsg}</Alert>}

<RHFTextField name="name" label="name" />
{/* <RHFTextField name="name" label="name" /> */}
<RHFTextField name="email" label="Email address" />

{/* <RHFTextField
<RHFTextField
name="password"
label="Password"
type={password.value ? 'text' : 'password'}
Expand All @@ -85,11 +104,7 @@ export default function JwtLoginView() {
</InputAdornment>
),
}}
/> */}

<Link variant="body2" color="inherit" underline="always" sx={{ alignSelf: 'flex-end' }}>
Forgot password?
</Link>
/>

<LoadingButton
fullWidth
Expand Down
4 changes: 2 additions & 2 deletions src/sections/home/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default function HomeView() {

useEffect(() => {
try {
GetFirstInfo().then((res) => {
login?.(res.data.name, res.data.email);
GetFirstInfo().then((res: any) => {
login?.(res.data.email, res.data.password);
setUserInfo({ email: res.data.email, name: res.data.name, hanRole: res.data.hanRole });
setDeptInfo(res.data.departmentResponses);
});
Expand Down
2 changes: 1 addition & 1 deletion src/sections/user/user-quick-edit-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function UserQuickEditForm({ currentUser, open, onClose, refetch
try {
await new Promise((resolve) => setTimeout(resolve, 500));
reset();
onClose();
// onClose();
enqueueSnackbar('Update success!');

// await axiosInstance.patch(endpoints.user.update, { data }).then((res) => {
Expand Down

0 comments on commit 2568d87

Please sign in to comment.