-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.mjs
72 lines (67 loc) · 2.1 KB
/
auth.mjs
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import express from "express";
import cookieParser from "cookie-parser";
import axios from "axios";
import { OAuth2Client } from "google-auth-library";
import dotenv from "dotenv";
dotenv.config();
const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID);
const authRouter = express.Router();
authRouter.post("/auth", async (req, res) => {
const { code } = req.body;
try {
const response = await axios.post(
"https://oauth2.googleapis.com/token",
{
code,
client_id: process.env.GOOGLE_CLIENT_ID,
client_secret: process.env.GOOGLE_CLIENT_SECRET,
redirect_uri: process.env.GOOGLE_REDIRECT_URI,
grant_type: "authorization_code",
},
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
},
);
const token = response.data.access_token;
res.status(200).json({ access_token: token });
} catch (error) {
console.error(
"Error exchanging code:",
error.response?.data || error.message,
);
res.status(500).json({ error: "Failed to exchange authorization code" });
}
});
authRouter.get("/me", async (req, res) => {
const token = req.cookies.access_token;
if (!token) {
return res.status(401).json({ error: "Unauthorized: No token provided" });
}
try {
// Use the token to fetch data from an external API
const response = await axios.get(
"https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);
res.json(response.data);
} catch (error) {
console.error("Failed to fetch protected data:", error.message);
res.status(500).json({ error: "Failed to fetch data" });
}
});
// authRouter.get("/logout", async (req, res) => {
// try {
// res.clearCookie('access_token', { path: '/' });
// res.status(200).json({ message: "Logged out successfully" })
// } catch (error) {
// console.error("Failed to fetch protected data:", error.message);
// res.status(500).json({ error: "Failed to fetch data" });
// }
// });
export default authRouter;