-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (62 loc) · 2.12 KB
/
server.js
File metadata and controls
76 lines (62 loc) · 2.12 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
// server.js
const express = require('express');
const axios = require('axios');
const dotenv = require('dotenv');
const firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
dotenv.config();
const app = express();
const port = 3000;
const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.FIREBASE_APP_ID,
};
firebase.initializeApp(firebaseConfig);
const githubClientId = process.env.GITHUB_CLIENT_ID;
const githubClientSecret = process.env.GITHUB_CLIENT_SECRET;
const githubRedirectUri = `http://localhost:${port}/auth/github/callback`;
app.get('/auth/github', (req, res) => {
const githubAuthUrl = `https://github.com/login/oauth/authorize?client_id=${githubClientId}&redirect_uri=${githubRedirectUri}&scope=user`;
res.redirect(githubAuthUrl);
});
app.get('/auth/github/callback', async (req, res) => {
const code = req.query.code;
try {
const { data } = await axios.post('https://github.com/login/oauth/access_token', {
client_id: githubClientId,
client_secret: githubClientSecret,
code: code,
redirect_uri: githubRedirectUri,
}, {
headers: {
Accept: 'application/json',
},
});
const accessToken = data.access_token;
const { data: userData } = await axios.get('https://api.github.com/user', {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
saveUserData(userData.id, {
githubUsername: userData.login,
email: userData.email,
photoURL: userData.avatar_url,
});
res.send('User authenticated successfully. You can close this window.');
} catch (error) {
console.error(error.message);
res.status(500).send('Error during GitHub authentication.');
}
});
function saveUserData(uid, data) {
firebase.database().ref('users/' + uid).set(data);
}
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});