Skip to content

Commit

Permalink
[W-LG002] 비밀번호 틀릴 시 에러 보이게끔 수정 및 기본 리다이렉트 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
zestlee1106 committed Dec 6, 2023
1 parent 78bf18c commit dcb9147
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
30 changes: 17 additions & 13 deletions pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ export default NextAuth({
return null;
}

const { email, password } = credentials;
try {
const { email, password } = credentials;

const data = await login({ email, password });
const data = await login({ email, password });

if (!data) {
return null;
}
if (!data) {
return null;
}

const jwt = parseJWT(data.accessToken);
const jwt = parseJWT(data.accessToken);

const user = {
id: jwt.email,
email: jwt.email,
token: data.accessToken,
exp: jwt.exp,
};
const user = {
id: jwt.email,
email: jwt.email,
token: data.accessToken,
exp: jwt.exp,
};

return user;
return user;
} catch (error) {
return null;
}
},
}),
],
Expand Down
20 changes: 13 additions & 7 deletions pages/login.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React from 'react';
import React, { useEffect } from 'react';
import type { GetStaticPropsContext } from 'next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { useTranslation } from 'next-i18next';
import { FieldError, useForm } from 'react-hook-form';
import { useRouter } from 'next/router';
import { LoginLayout, Link, CustomImage, Chip, Button, Input, Space } from '@/components/index.tsx';
import { LoginLayout, Link, CustomImage, Button, Input, Space } from '@/components/index.tsx';
import { isRequired, isValidEmail, isValidPassword } from '@/utils/validCheck.ts';
import { login } from '@/api/signup';
import { signIn } from 'next-auth/react';
import { getProfile } from '@/api/userInfo';
import useUserInfo from '@/hooks/useUserInfo.ts';
import { useRouter } from 'next/router';

export const getStaticProps = async ({ locale }: GetStaticPropsContext) => ({
props: {
Expand All @@ -25,6 +24,7 @@ export default function Login() {
register,
formState: { errors },
watch,
setError,
} = useForm({ mode: 'onChange' });

const email = watch('email');
Expand All @@ -41,13 +41,19 @@ export default function Login() {
}
};

const router = useRouter();
const onSubmit = async () => {
await selectProfile();
await signIn('email-password-credential', {
const data = await signIn('email-password-credential', {
email,
password,
callbackUrl: '/',
redirect: false,
});
if (data?.status !== 200) {
setError('password', { type: 'validate', message: 'Invalid email or password' });
return;
}
await selectProfile();
router.push('/');
};

const isDisabledButton = !email || !password || !isValidEmail(email) || !isValidPassword(password);
Expand Down

0 comments on commit dcb9147

Please sign in to comment.