Skip to content

Commit 092d66c

Browse files
committed
Fix auth login logic: avoid password rehashing and token-gated auth
1 parent f4a9673 commit 092d66c

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/components/Signin.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ const Signin = () => {
3636
setIsPasswordVisible((prevState: any) => !prevState);
3737
}
3838
const router = useRouter();
39-
const email = useRef('');
40-
const password = useRef('');
39+
// const email = useRef('');
40+
// const password = useRef('');
41+
const [email, setEmail] = useState('');
42+
const [password, setPassword] = useState('');
4143

4244
const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => {
4345
const value = e.target.value;
44-
email.current = value;
46+
// email.current = value.trim();
47+
setEmail(value);
4548

4649
setFocusedIndex(0);
4750
setRequiredError((prevState) => ({
@@ -86,9 +89,9 @@ const Signin = () => {
8689
};
8790

8891
const handleSuggestionClick = (domain: string) => {
89-
const [username] = email.current.split('@');
92+
const [username] = email.split('@');
9093
const newEmail = `${username}@${domain}`;
91-
email.current = newEmail;
94+
setEmail(newEmail);
9295
passwordRef.current?.focus();
9396
setSuggestedDomains([]);
9497
};
@@ -114,21 +117,21 @@ const Signin = () => {
114117
e.preventDefault();
115118
}
116119

117-
if (!email.current || !password.current) {
120+
if (!email || !password) {
118121
setRequiredError({
119-
emailReq: email.current ? false : true,
120-
passReq: password.current ? false : true,
122+
emailReq: email ? false : true,
123+
passReq: password ? false : true,
121124
});
122125
toast.dismiss(loadId);
123126
return;
124127
}
125128
setCheckingPassword(true);
126129
const res = await signIn('credentials', {
127-
username: email.current,
128-
password: password.current,
130+
username: email.trim(),
131+
password: password,
129132
redirect: false,
130133
});
131-
134+
console.log('SignIn Response:', res);
132135
toast.dismiss(loadId);
133136
if (!res?.error) {
134137
router.push('/');
@@ -199,12 +202,12 @@ const Signin = () => {
199202
name="email"
200203
id="email"
201204
placeholder="[email protected]"
202-
value={email.current}
205+
value={email}
203206
onChange={handleEmailChange}
204207
onKeyDown={handleKeyDown}
205208
onBlur={() => setSuggestedDomains([])} // Hide suggestions on blur
206209
/>
207-
{email.current && suggestedDomains.length > 0 && (
210+
{email && suggestedDomains.length > 0 && (
208211
<ul
209212
ref={dropdownRef}
210213
className={`absolute top-20 z-50 max-h-96 w-full min-w-[8rem] overflow-auto rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2`}
@@ -226,7 +229,7 @@ const Signin = () => {
226229
: ''
227230
}`}
228231
>
229-
{email.current.split('@')[0]}@{domain}
232+
{email.split('@')[0]}@{domain}
230233
</li>
231234
{index < suggestedDomains.length - 1 && <Separator />}
232235
</>
@@ -252,7 +255,7 @@ const Signin = () => {
252255
...prevState,
253256
passReq: false,
254257
}));
255-
password.current = e.target.value;
258+
setPassword(e.target.value);
256259
}}
257260
onKeyDown={async (e) => {
258261
if (e.key === 'Enter') {
@@ -311,7 +314,7 @@ const Signin = () => {
311314
<Button
312315
size={'lg'}
313316
variant={'branding'}
314-
disabled={!email.current || !password.current || checkingPassword}
317+
disabled={!email || !password || checkingPassword}
315318
onClick={handleSubmit}
316319
>
317320
Login

src/lib/auth.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,15 @@ async function validateUser(
114114
export const authOptions = {
115115
providers: [
116116
CredentialsProvider({
117+
id: 'credentials',
117118
name: 'Credentials',
118119
credentials: {
119120
username: { label: 'email', type: 'text', placeholder: '' },
120121
password: { label: 'password', type: 'password', placeholder: '' },
121122
},
122123
async authorize(credentials: any) {
124+
console.log("LOCAL_CMS_PROVIDER =", process.env.LOCAL_CMS_PROVIDER);
125+
123126
try {
124127
if (process.env.LOCAL_CMS_PROVIDER) {
125128
return {
@@ -131,7 +134,7 @@ export const authOptions = {
131134
}),
132135
};
133136
}
134-
const hashedPassword = await bcrypt.hash(credentials.password, 10);
137+
// const hashedPassword = await bcrypt.hash(credentials.password, 10);
135138

136139
const userDb = await prisma.user.findFirst({
137140
where: {
@@ -147,8 +150,7 @@ export const authOptions = {
147150
if (
148151
userDb &&
149152
userDb.password &&
150-
(await bcrypt.compare(credentials.password, userDb.password)) &&
151-
userDb?.appxAuthToken
153+
await bcrypt.compare(credentials.password, userDb.password)
152154
) {
153155
const jwt = await generateJWT({
154156
id: userDb.id,
@@ -180,6 +182,10 @@ export const authOptions = {
180182
});
181183

182184
if (user.data) {
185+
const hashedPassword = await bcrypt.hash(
186+
credentials.password,
187+
10,
188+
)
183189
try {
184190
await db.user.upsert({
185191
where: {

0 commit comments

Comments
 (0)