Skip to content

Commit f1560e7

Browse files
Updated to be compatible with frontend
1 parent dfe93d1 commit f1560e7

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.env
3+
.next

backend/api.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,38 @@ servers:
99
description: Development server
1010

1111
paths:
12+
/verify:
13+
get:
14+
summary: Verify access token
15+
security:
16+
- bearerAuth: []
17+
responses:
18+
200:
19+
description: Access token verified
20+
content:
21+
application/json:
22+
schema:
23+
type: object
24+
properties:
25+
status:
26+
type: integer
27+
example: 200
28+
message:
29+
type: string
30+
example: Verified
31+
401:
32+
description: Unauthorized
33+
content:
34+
application/json:
35+
schema:
36+
type: object
37+
properties:
38+
status:
39+
type: integer
40+
example: 401
41+
message:
42+
type: string
43+
example: Unauthorized
1244
/register:
1345
post:
1446
summary: Register a new user

backend/src/routes/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ app.post('/login', LoginValidate, async (req, res) => {
2929
const token = generateAccessToken(email, user.username)
3030
const rt = await generateRefreshToken(email, user.username)
3131
await sendLoginMail(user.email, user.username)
32-
res.status(200).send({status: 200, message: "User logged in", access_token: token, refresh_token: rt.token})
32+
res.status(200).send({status: 200, message: "User logged in", userId: user._id, name: user.username, access_token: token, refresh_token: rt.token})
3333
} catch(_) {
3434
res.status(500).send({status: 500, message: "Unknown Error"})
3535
}

backend/src/routes/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ app.post('/register', RegisterValidate, async (req, res) => {
2727
const token = generateAccessToken(email, username)
2828
const rt = await generateRefreshToken(email, username)
2929
const pwd = await Bun.password.hash(password)
30-
await users.insertOne({email, username, password:pwd})
30+
const newUser = await users.insertOne({email, username, password:pwd})
3131
await sendRegMail(email, username)
32-
res.status(201).send({status:201, message: "User Created", access_token: token, refresh_token: rt.token})
32+
res.status(201).send({status:201, message: "User Created", userId: newUser.insertedId, access_token: token, refresh_token: rt.token})
3333
} catch(_) {
3434
console.error(_);
3535

backend/src/routes/verify.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Router} from 'express'
2+
import { verifyAccessToken } from '../utils/tokens'
3+
4+
const app = Router()
5+
6+
app.get('/verify', (req, res) => {
7+
if(verifyAccessToken(req.headers.authorization?.split(" ")[0] as string)) {
8+
res.status(200).send({status: 200, message: "OK"})
9+
} else {
10+
res.status(401).send({status: 401, message: "Invalid token"})
11+
}
12+
})

backend/src/utils/tokens.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ export const refreshAccessToken = async (refreshToken: string) => {
1717
if(!t) throw Error("Invalid refresh token");
1818
if(t.expiresAt < new Date()) throw Error("Refresh token expired");
1919
return sign({username, email}, jwt_secret, {expiresIn: 15*60})
20+
}
21+
22+
export const verifyAccessToken = (accessToken: string) => {
23+
return verify(accessToken, jwt_secret)
2024
}

0 commit comments

Comments
 (0)