forked from rpp32-boc-igneous/streamFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.js
71 lines (63 loc) · 2.11 KB
/
oauth.js
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
const express = require("express");
const router = express.Router();
const google_oauth = require("./google_auth.js");
const cors = require("cors");
const { User } = require("../database/index.js");
router.use(cors());
router.use((req, res, next) => {
console.log("The router has been used: " + Date.now());
next();
});
// Entrance to the validated portion of the app
router.get("/openSesame", (req, res) => {
let id_cookie = JSON.parse(req.cookies.id);
res.send(id_cookie);
});
// Getting the user from Google with the code
router.get("/google", (req, res) => {
const url = google_oauth.get_url();
console.log("This is the access code URL: ", url);
res.send(url);
});
////////////////////////////
// Getting the current user :::: Not working Currently
////////////////////////////
router.get("/google/redirect", (req, res) => {
// I will need to check the DB here and see if there is an existing user..
// This currently isnt being used. Need to figure this out or delete it...
console.log("Just want to check and see if this is running??");
// let id_info =
google_oauth
.get_code(code)
.then((code) => {
let id_info = JSON.stringify(code);
res.cookie("id", id_info).redirect("/oauth/openSesame");
})
.catch((error) => res.send(error));
});
// Checks the DB for an existing user and returns it if currently exists
// If new user, new entry is added to the Users collection and returned
// If user is not in the DB we need to redirect the user to the Sign up page
router.get("/verifyUser", (req, res) => {
let query = { email: req.query.email };
let user = {
email: req.query.email,
name: req.query.name,
first_name: req.query.first_name,
};
let options = { upsert: true };
User.findOne(query)
.then((user) => {
console.log("This is the findOne result: ", user);
if (user === null) {
console.log("User does not exist, redirecting to sign up");
res.send(false);
} else {
res.send(user);
}
})
.catch((error) => error);
});
// router.get("/facebook", AOL);
// roter.get("/myspace", myspace);
module.exports = router;