Skip to content

Commit cc334b6

Browse files
committed
admin access token
1 parent 9edf3e6 commit cc334b6

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

server_node/controller/auth.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ const register =async (req, res) => {
99

1010
const existingUser = await auth.findOne({ email })
1111
if(existingUser){
12-
return res.status(400).json({ message: "User exists"});
12+
return res.status(400).json({ message: "User exists "});
1313
}
1414

1515
const hashedPassword = await bcrypt.hash(password, 12)
16-
1716
const User = new auth({
1817
username,
1918
firstName,
@@ -22,6 +21,7 @@ const register =async (req, res) => {
2221
phone_number,
2322
password: hashedPassword
2423
})
24+
2525
await User.save();
2626
res.status(201).json({ message: 'User registered successfully' });
2727
}catch(error){
@@ -30,7 +30,7 @@ const register =async (req, res) => {
3030
}
3131
}
3232

33-
const login = async(req, res) => {
33+
const login = async (req, res) => {
3434
try{
3535
const { email, password} = req.body;
3636

@@ -40,9 +40,20 @@ const login = async(req, res) => {
4040
const isMatch = await bcrypt.compare(password, auth.password);
4141
if(!isMatch) return res.status(400).json({message : "Incorrect Password"})
4242

43-
const token = jwt.sign({id: user._id}, process.env.JWT_SECRET, {expiresIn: '2h'});
43+
const access_token = jwt.sign({
44+
user_id: user_id,
45+
},
46+
process.env.JWT_SECRET,{
47+
expiresIn: '1h'
48+
}
49+
)
50+
51+
//const token = jwt.sign({id: user._id}, process.env.JWT_SECRET, {expiresIn: '2h'});
4452

45-
res.json({ message: 'Login successful', token });
53+
res.status(200).json({
54+
message: 'Login successful',
55+
access_token
56+
});
4657
}catch(error){
4758
res.status(500).json({message: "Error loging in"});
4859
console.log("server error loging in", error);

0 commit comments

Comments
 (0)