1
1
import bcrypt from "bcryptjs" ;
2
2
import jwt from "jsonwebtoken" ;
3
- import { findUserByEmail as _findUserByEmail } from "../model/repository.js" ;
3
+ import { findUserByUsername as _findUserByUsername } from "../model/repository.js" ;
4
4
import { formatUserResponse } from "./user-controller.js" ;
5
5
6
6
export async function handleLogin ( req , res ) {
7
- const { email , password } = req . body ;
8
- if ( email && password ) {
7
+ const { username , password } = req . body ;
8
+ if ( username && password ) {
9
9
try {
10
- const user = await _findUserByEmail ( email ) ;
10
+ const user = await _findUserByUsername ( username ) ;
11
11
if ( ! user ) {
12
- return res . status ( 401 ) . json ( { message : "Wrong email and/or password" } ) ;
12
+ return res . status ( 401 ) . json ( { message : "Wrong username and/or password" } ) ;
13
13
}
14
14
15
15
const match = await bcrypt . compare ( password , user . password ) ;
16
16
if ( ! match ) {
17
- return res . status ( 401 ) . json ( { message : "Wrong email and/or password" } ) ;
17
+ return res . status ( 401 ) . json ( { message : "Wrong username and/or password" } ) ;
18
18
}
19
19
20
20
const accessToken = jwt . sign ( {
@@ -27,7 +27,7 @@ export async function handleLogin(req, res) {
27
27
return res . status ( 500 ) . json ( { message : err . message } ) ;
28
28
}
29
29
} else {
30
- return res . status ( 400 ) . json ( { message : "Missing email and/or password" } ) ;
30
+ return res . status ( 400 ) . json ( { message : "Missing username and/or password" } ) ;
31
31
}
32
32
}
33
33
0 commit comments