-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.controller.js
More file actions
35 lines (29 loc) · 1002 Bytes
/
Copy pathauth.controller.js
File metadata and controls
35 lines (29 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import ApiResponse from "../../utils/api-response.js";
import * as AuthService from "./auth.service.js";
export function health(req, res) {
return ApiResponse.ok(res, "Auth Up and Running");
}
export async function logout(req, res) {
await AuthService.logout(req);
res.clearCookie("connect.sid");
return res.redirect("/login");
}
export async function me(req, res) {
const user = AuthService.getMe(req);
return res.json({ user });
}
export async function callback(req, res) {
const { code } = req.query;
if (!code) return res.status(400).json({ error: "No auth code" });
try {
await AuthService.exchangeAuthCode(req, code);
return res.redirect("/");
} catch (err) {
return res.status(500).json({ error: "Token exchange failed" });
}
}
export async function userInfo(req, res) {
const user = await AuthService.fetchUserInfo(req);
if (!user) return res.status(401).json({ error: "No user" });
return res.json(user);
}