-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (80 loc) · 3.01 KB
/
Copy pathserver.js
File metadata and controls
97 lines (80 loc) · 3.01 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require("express");
const axios = require('axios');
const cors = require('cors');
const { OAuth2Client } = require('google-auth-library');
const oauth2Client = new OAuth2Client()
// Create an Express app and listen for incoming requests on port 3000
const app = express();
const router = express.Router();
const port = process.env.PORT || 3000;
// static files
app.use(express.static('files'))
// Enable CORS for all routes
app.use(cors());
// 1. Call the Google SDK from the frontend using whatever frontend
//2. Extract the code or access token and send to your backend for verification.
//3. Use your backend Google api to verify the code or token.
//4. If verified, sign them in the backend and then send a response to frontend
app.post('/auth', async (req, res) => {
try {
// get the code from frontend
const code = req.headers.authorization;
console.log('Authorization Code:', code);
// Exchange the authorization code for an access token
const response = await axios.post(
'https://oauth2.googleapis.com/token',
{
code,
client_id: '587301-d27f8hofgi6i0.apps.googleusercontent.com',
client_secret: 'GOCSPX-u02eNWutQVi',
redirect_uri: 'postmessage',
grant_type: 'authorization_code'
}
);
const accessToken = response.data.access_token;
console.log('Access Token:', accessToken);
// Fetch user details using the access token
const userResponse = await axios.get(
'https://www.googleapis.com/oauth2/v3/userinfo',
{
headers: {
Authorization: `Bearer ${accessToken}`
}
}
);
const userDetails = userResponse.data;
console.log('User Details:', userDetails);
// Process user details and perform necessary actions
res.status(200).json({ message: 'Authentication successful' });
} catch (error) {
console.error('Error saving code:', error);
res.status(500).json({ message: 'Failed to save code' });
}
});
// Use middleware to parse incoming requests with JSON and URL-encoded payloads
app.use(express.json());
app.use(express.urlencoded());
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Internal Server Error");
});
// Handle GET requests to the root URL
router.get("/", (req, res) => {
res.send("Welcome to the Webhook Server!");
});
// Handle POST requests to specific URLs i.e. webhook endpoints
router.post("/webhook-1", (req, res) => {
console.log(req.body);
res.send("Webhook 1 successfully received.");
});
router.post("/webhook-2", (req, res) => {
console.log(req.body);
res.send("Webhook 2 successfully received.");
});
// Mount the router middleware
app.use(router);
// Start the server and listen for incoming connections
app.listen(port, () => {
console.log(`Server running at https://localhost:${port}/`);
});