Added Profile and updated Schema
This commit is contained in:
parent
52f39cf3ae
commit
6d6705e191
62
app.js
62
app.js
|
@ -2,14 +2,76 @@ const express = require('express') ;
|
|||
const mongoose = require('mongoose') ;
|
||||
const bodyparser = require('body-parser') ;
|
||||
const authRoute = require('./routes/auth') ;
|
||||
const profileRoute = require('./routes/profile') ;
|
||||
//const passport = require('passport');
|
||||
//const cookieSession = require('cookie-session') ;
|
||||
//require('./passport-setup') ;
|
||||
|
||||
const app = express() ;
|
||||
|
||||
const MONGO_URI = `mongodb+srv://Cantilever:Cantilever@cluster0.dqxva.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`
|
||||
|
||||
app.use(bodyparser.json()) ;
|
||||
|
||||
// app.use(cookieSession({
|
||||
// name: 'test-session',
|
||||
// keys: ['key1', 'key2']
|
||||
// }))
|
||||
|
||||
// const isLoggedIn = (req , res , next) => {
|
||||
// if(req.user)
|
||||
// {
|
||||
// next()
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// res.json({
|
||||
// error : "No user"
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
// app.use(passport.initialize());
|
||||
// app.use(passport.session());
|
||||
|
||||
|
||||
// app.get('/failed' , (req , res) => {
|
||||
// res.json({
|
||||
// error : "You have failed to login"
|
||||
// })
|
||||
// })
|
||||
|
||||
// app.get('/' , (req , res) => {
|
||||
// console.log('you are not logged in');
|
||||
|
||||
// })
|
||||
|
||||
// app.get('/good' ,isLoggedIn , (req , res) => {
|
||||
// res.json({
|
||||
// message:"success" ,
|
||||
// user : req.user
|
||||
// })
|
||||
// })
|
||||
|
||||
// app.get('/auth/google',
|
||||
// passport.authenticate('google', { scope: ['profile' , 'email'] }));
|
||||
|
||||
// app.get('/auth/google/callback',
|
||||
// passport.authenticate('google', { failureRedirect: '/failed' }),
|
||||
// function(req, res) {
|
||||
// res.redirect('/good');
|
||||
// });
|
||||
|
||||
// app.get('/logout', (req , res) => {
|
||||
// req.session = null ;
|
||||
// req.logout() ;
|
||||
// res.redirect('/') ;
|
||||
// })
|
||||
|
||||
app.use(authRoute) ;
|
||||
|
||||
app.use(profileRoute) ;
|
||||
|
||||
mongoose.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }).
|
||||
then(result => {
|
||||
console.log('connected');
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
const bcrypt = require('bcryptjs') ;
|
||||
const User = require('../models/User') ;
|
||||
const Student = require('../models/Student') ;
|
||||
const jwt = require('jsonwebtoken');
|
||||
const JWT_secret = "Cantileverlabs";
|
||||
|
||||
module.exports.Protected = async (req,res,next)=>{
|
||||
res.send("Hello User")
|
||||
}
|
||||
module.exports.postSignup = async (req , res , next) => {
|
||||
try
|
||||
{
|
||||
let firstName = req.body.firstName || " " ;
|
||||
let lastName = req.body.lastName || " " ;
|
||||
let email = req.body.email ;
|
||||
let phoneNumber = req.body.phoneNumber ;
|
||||
let password = req.body.password ;
|
||||
let user = await User.findOne({email:email}) ;
|
||||
if(user)
|
||||
|
@ -24,10 +29,14 @@ module.exports.postSignup = async (req , res , next) => {
|
|||
firstName : firstName ,
|
||||
lastName : lastName ,
|
||||
email : email ,
|
||||
password : hashedPass ,
|
||||
phoneNumber : phoneNumber
|
||||
password : hashedPass
|
||||
}) ;
|
||||
user = await user.save() ;
|
||||
await Student.deleteOne({user:user._id}) ;
|
||||
let student = new Student({
|
||||
user:user._id
|
||||
})
|
||||
await student.save() ;
|
||||
res.json({
|
||||
message:"Successfully signed Up" ,
|
||||
type:"success"
|
||||
|
@ -49,13 +58,15 @@ module.exports.postSignin = async (req , res , next) => {
|
|||
let user = await User.findOne({email : email}) ;
|
||||
if(user)
|
||||
{
|
||||
const isMathced = await bcrypt.compare(password , user.password) ;
|
||||
if(isMathced)
|
||||
const isMatched = await bcrypt.compare(password , user.password) ;
|
||||
if(isMatched)
|
||||
{
|
||||
res.json({
|
||||
message:"Logged In" ,
|
||||
type : "success"
|
||||
})
|
||||
const token = jwt.sign({_id:user._id},JWT_secret)
|
||||
res.json(
|
||||
{
|
||||
token:token
|
||||
}
|
||||
)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -78,3 +89,11 @@ module.exports.postSignin = async (req , res , next) => {
|
|||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.checkProtected = (req , res , next) => {
|
||||
console.log(req.user);
|
||||
res.json({
|
||||
message:"Protected" ,
|
||||
user : req.user
|
||||
})
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
const Student = require('../models/Student') ;
|
||||
const User = require('../models/User') ;
|
||||
|
||||
function trim_arr(arr)
|
||||
{
|
||||
let newArr = arr.map(a => {
|
||||
return a.trim() ;
|
||||
})
|
||||
return newArr ;
|
||||
}
|
||||
|
||||
module.exports.getProfile =async (req , res , next) => {
|
||||
try
|
||||
{
|
||||
const userId = req.user._id ;
|
||||
let user = await User.findById(userId) ;
|
||||
let student = await Student.findOne({user : userId}) ;
|
||||
res.json({
|
||||
user:user ,
|
||||
student:student
|
||||
}) ;
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
res.json({
|
||||
error:err
|
||||
}) ;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.postProfile = async (req , res , next) => {
|
||||
try
|
||||
{
|
||||
const userId = req.user._id ;
|
||||
|
||||
let courses = req.body.courses ;
|
||||
let interests = req.body.interests ;
|
||||
let projects = req.body.projects ;
|
||||
let yearofgrad = req.body.yearofgrad ;
|
||||
let phoneNumber = req.body.phoneNumber ;
|
||||
let institute = req.body.institute ;
|
||||
let skills = req.body.skills ;
|
||||
|
||||
let student = await Student.findOne({user : userId}) ;
|
||||
if(courses)
|
||||
{
|
||||
student.courses = trim_arr(courses.split(",")) ;
|
||||
}
|
||||
if(interests)
|
||||
{
|
||||
student.interests = trim_arr(interests.split(",")) ;
|
||||
}
|
||||
if(projects)
|
||||
{
|
||||
student.projects = trim_arr(projects.split(",")) ;
|
||||
}
|
||||
if(skills)
|
||||
{
|
||||
student.skills = trim_arr(skills.split(",")) ;
|
||||
}
|
||||
if(yearofgrad)
|
||||
{
|
||||
student.yearofgrad = yearofgrad.trim() ;
|
||||
}
|
||||
if(phoneNumber)
|
||||
{
|
||||
student.phoneNumber = phoneNumber.trim() ;
|
||||
}
|
||||
if(institute)
|
||||
{
|
||||
student.institute = institute.trim() ;
|
||||
}
|
||||
await student.save() ;
|
||||
|
||||
res.json({
|
||||
message:"Updated profile"
|
||||
})
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
console.log(err);
|
||||
res.json({
|
||||
error:err
|
||||
}) ;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
const jwt = require('jsonwebtoken')
|
||||
const JWT_secret = "Cantileverlabs"
|
||||
const mongoose = require('mongoose')
|
||||
const User = mongoose.model("User")
|
||||
module.exports = async (req,res,next)=>{
|
||||
const {authorization} = req.headers ;
|
||||
//authorization === Bearer Cantileverlabs
|
||||
if(!authorization){
|
||||
return res.status(401).json({error:"You must be logged in"})
|
||||
}
|
||||
const token = authorization.replace("Bearer ","")
|
||||
jwt.verify(token,JWT_secret,async (err,payload)=>{
|
||||
if(err){
|
||||
return res.status(401).json({error:"You must be logged in"}) ;
|
||||
}
|
||||
const {_id} = payload ;
|
||||
const user = await User.findById(_id) ;
|
||||
if(user)
|
||||
{
|
||||
req.user = user ;
|
||||
next() ;
|
||||
}
|
||||
else
|
||||
{
|
||||
return res.status(401).json({error:"No user with these credentials exist"}) ;
|
||||
}
|
||||
})
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
const mongoose = require('mongoose') ;
|
||||
|
||||
const Schema = mongoose.Schema ;
|
||||
|
||||
const studentSchema = new Schema({
|
||||
institute:{
|
||||
type:String
|
||||
},
|
||||
yearofgrad:{
|
||||
type:String
|
||||
},
|
||||
phoneNumber : {
|
||||
type : String ,
|
||||
} ,
|
||||
courses:[
|
||||
{
|
||||
type:String
|
||||
}
|
||||
],
|
||||
interests:[
|
||||
{
|
||||
type : String ,
|
||||
}
|
||||
],
|
||||
projects:[
|
||||
{
|
||||
type : String ,
|
||||
}
|
||||
],
|
||||
skills:[
|
||||
{
|
||||
type:String ,
|
||||
}
|
||||
] ,
|
||||
user : {
|
||||
type:mongoose.Types.ObjectId ,
|
||||
ref:'User' ,
|
||||
required:true
|
||||
}
|
||||
}) ;
|
||||
|
||||
module.exports = mongoose.model('Student' , studentSchema) ;
|
|
@ -15,13 +15,11 @@ const userSchema = new Schema({
|
|||
type:String ,
|
||||
required: true
|
||||
} ,
|
||||
phoneNumber : {
|
||||
type : String ,
|
||||
required : true
|
||||
} ,
|
||||
password : {
|
||||
type : String ,
|
||||
required : true
|
||||
type : String
|
||||
} ,
|
||||
googleId : {
|
||||
type : String
|
||||
}
|
||||
}) ;
|
||||
|
||||
|
|
|
@ -114,6 +114,11 @@
|
|||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
|
||||
"dev": true
|
||||
},
|
||||
"base64url": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/base64url/-/base64url-3.0.1.tgz",
|
||||
"integrity": "sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A=="
|
||||
},
|
||||
"bcryptjs": {
|
||||
"version": "2.4.3",
|
||||
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
|
||||
|
@ -196,6 +201,11 @@
|
|||
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.6.tgz",
|
||||
"integrity": "sha512-EvVNVeGo4tHxwi8L6bPj3y3itEvStdwvvlojVxxbyYfoaxJ6keLgrTuKdyfEAszFK+H3olzBuafE0yoh0D1gdg=="
|
||||
},
|
||||
"buffer-equal-constant-time": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
|
||||
"integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
|
||||
},
|
||||
"bytes": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
|
||||
|
@ -356,11 +366,37 @@
|
|||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
|
||||
"integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
|
||||
},
|
||||
"cookie-session": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/cookie-session/-/cookie-session-1.4.0.tgz",
|
||||
"integrity": "sha512-0hhwD+BUIwMXQraiZP/J7VP2YFzqo6g4WqZlWHtEHQ22t0MeZZrNBSCxC1zcaLAs8ApT3BzAKizx9gW/AP9vNA==",
|
||||
"requires": {
|
||||
"cookies": "0.8.0",
|
||||
"debug": "2.6.9",
|
||||
"on-headers": "~1.0.2"
|
||||
}
|
||||
},
|
||||
"cookie-signature": {
|
||||
"version": "1.0.6",
|
||||
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
|
||||
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
|
||||
},
|
||||
"cookies": {
|
||||
"version": "0.8.0",
|
||||
"resolved": "https://registry.npmjs.org/cookies/-/cookies-0.8.0.tgz",
|
||||
"integrity": "sha512-8aPsApQfebXnuI+537McwYsDtjVxGm8gTIzQI3FDW6t5t/DAhERxtnbEPN/8RX+uZthoz4eCOgloXaE5cYyNow==",
|
||||
"requires": {
|
||||
"depd": "~2.0.0",
|
||||
"keygrip": "~1.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"depd": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
|
||||
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"core-util-is": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
|
||||
|
@ -431,6 +467,14 @@
|
|||
"integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
|
||||
"dev": true
|
||||
},
|
||||
"ecdsa-sig-formatter": {
|
||||
"version": "1.0.11",
|
||||
"resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
|
||||
"integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
|
||||
"requires": {
|
||||
"safe-buffer": "^5.0.1"
|
||||
}
|
||||
},
|
||||
"ee-first": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
|
||||
|
@ -769,11 +813,62 @@
|
|||
"integrity": "sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg=",
|
||||
"dev": true
|
||||
},
|
||||
"jsonwebtoken": {
|
||||
"version": "8.5.1",
|
||||
"resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz",
|
||||
"integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==",
|
||||
"requires": {
|
||||
"jws": "^3.2.2",
|
||||
"lodash.includes": "^4.3.0",
|
||||
"lodash.isboolean": "^3.0.3",
|
||||
"lodash.isinteger": "^4.0.4",
|
||||
"lodash.isnumber": "^3.0.3",
|
||||
"lodash.isplainobject": "^4.0.6",
|
||||
"lodash.isstring": "^4.0.1",
|
||||
"lodash.once": "^4.0.0",
|
||||
"ms": "^2.1.1",
|
||||
"semver": "^5.6.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"ms": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
||||
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"jwa": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
|
||||
"integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
|
||||
"requires": {
|
||||
"buffer-equal-constant-time": "1.0.1",
|
||||
"ecdsa-sig-formatter": "1.0.11",
|
||||
"safe-buffer": "^5.0.1"
|
||||
}
|
||||
},
|
||||
"jws": {
|
||||
"version": "3.2.2",
|
||||
"resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
|
||||
"integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
|
||||
"requires": {
|
||||
"jwa": "^1.4.1",
|
||||
"safe-buffer": "^5.0.1"
|
||||
}
|
||||
},
|
||||
"kareem": {
|
||||
"version": "2.3.2",
|
||||
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.2.tgz",
|
||||
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
|
||||
},
|
||||
"keygrip": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/keygrip/-/keygrip-1.1.0.tgz",
|
||||
"integrity": "sha512-iYSchDJ+liQ8iwbSI2QqsQOvqv58eJCEanyJPJi+Khyu8smkcKSFUCbPwzFcL7YVtZ6eONjqRX/38caJ7QjRAQ==",
|
||||
"requires": {
|
||||
"tsscmp": "1.0.6"
|
||||
}
|
||||
},
|
||||
"keyv": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz",
|
||||
|
@ -792,6 +887,41 @@
|
|||
"package-json": "^6.3.0"
|
||||
}
|
||||
},
|
||||
"lodash.includes": {
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
|
||||
"integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
|
||||
},
|
||||
"lodash.isboolean": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
|
||||
"integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
|
||||
},
|
||||
"lodash.isinteger": {
|
||||
"version": "4.0.4",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
|
||||
"integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
|
||||
},
|
||||
"lodash.isnumber": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
|
||||
"integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
|
||||
},
|
||||
"lodash.isplainobject": {
|
||||
"version": "4.0.6",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
|
||||
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
|
||||
},
|
||||
"lodash.isstring": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
|
||||
"integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
|
||||
},
|
||||
"lodash.once": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
|
||||
"integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
|
||||
},
|
||||
"lowercase-keys": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",
|
||||
|
@ -1017,6 +1147,11 @@
|
|||
"integrity": "sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==",
|
||||
"dev": true
|
||||
},
|
||||
"oauth": {
|
||||
"version": "0.9.15",
|
||||
"resolved": "https://registry.npmjs.org/oauth/-/oauth-0.9.15.tgz",
|
||||
"integrity": "sha1-vR/vr2hslrdUda7VGWQS/2DPucE="
|
||||
},
|
||||
"on-finished": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
|
||||
|
@ -1025,6 +1160,11 @@
|
|||
"ee-first": "1.1.1"
|
||||
}
|
||||
},
|
||||
"on-headers": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
|
||||
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA=="
|
||||
},
|
||||
"once": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
||||
|
@ -1065,11 +1205,77 @@
|
|||
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
|
||||
"integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ=="
|
||||
},
|
||||
"passport": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/passport/-/passport-0.4.1.tgz",
|
||||
"integrity": "sha512-IxXgZZs8d7uFSt3eqNjM9NQ3g3uQCW5avD8mRNoXV99Yig50vjuaez6dQK2qC0kVWPRTujxY0dWgGfT09adjYg==",
|
||||
"requires": {
|
||||
"passport-strategy": "1.x.x",
|
||||
"pause": "0.0.1"
|
||||
}
|
||||
},
|
||||
"passport-google-oauth": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/passport-google-oauth/-/passport-google-oauth-2.0.0.tgz",
|
||||
"integrity": "sha512-JKxZpBx6wBQXX1/a1s7VmdBgwOugohH+IxCy84aPTZNq/iIPX6u7Mqov1zY7MKRz3niFPol0KJz8zPLBoHKtYA==",
|
||||
"requires": {
|
||||
"passport-google-oauth1": "1.x.x",
|
||||
"passport-google-oauth20": "2.x.x"
|
||||
}
|
||||
},
|
||||
"passport-google-oauth1": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/passport-google-oauth1/-/passport-google-oauth1-1.0.0.tgz",
|
||||
"integrity": "sha1-r3SoA99R7GRvZqRNgigr5vEI4Mw=",
|
||||
"requires": {
|
||||
"passport-oauth1": "1.x.x"
|
||||
}
|
||||
},
|
||||
"passport-google-oauth20": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/passport-google-oauth20/-/passport-google-oauth20-2.0.0.tgz",
|
||||
"integrity": "sha512-KSk6IJ15RoxuGq7D1UKK/8qKhNfzbLeLrG3gkLZ7p4A6DBCcv7xpyQwuXtWdpyR0+E0mwkpjY1VfPOhxQrKzdQ==",
|
||||
"requires": {
|
||||
"passport-oauth2": "1.x.x"
|
||||
}
|
||||
},
|
||||
"passport-oauth1": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/passport-oauth1/-/passport-oauth1-1.1.0.tgz",
|
||||
"integrity": "sha1-p96YiiEfnPRoc3cTDqdN8ycwyRg=",
|
||||
"requires": {
|
||||
"oauth": "0.9.x",
|
||||
"passport-strategy": "1.x.x",
|
||||
"utils-merge": "1.x.x"
|
||||
}
|
||||
},
|
||||
"passport-oauth2": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.5.0.tgz",
|
||||
"integrity": "sha512-kqBt6vR/5VlCK8iCx1/KpY42kQ+NEHZwsSyt4Y6STiNjU+wWICG1i8ucc1FapXDGO15C5O5VZz7+7vRzrDPXXQ==",
|
||||
"requires": {
|
||||
"base64url": "3.x.x",
|
||||
"oauth": "0.9.x",
|
||||
"passport-strategy": "1.x.x",
|
||||
"uid2": "0.0.x",
|
||||
"utils-merge": "1.x.x"
|
||||
}
|
||||
},
|
||||
"passport-strategy": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/passport-strategy/-/passport-strategy-1.0.0.tgz",
|
||||
"integrity": "sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ="
|
||||
},
|
||||
"path-to-regexp": {
|
||||
"version": "0.1.7",
|
||||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
|
||||
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
|
||||
},
|
||||
"pause": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/pause/-/pause-0.0.1.tgz",
|
||||
"integrity": "sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10="
|
||||
},
|
||||
"picomatch": {
|
||||
"version": "2.2.2",
|
||||
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz",
|
||||
|
@ -1444,6 +1650,11 @@
|
|||
"nopt": "~1.0.10"
|
||||
}
|
||||
},
|
||||
"tsscmp": {
|
||||
"version": "1.0.6",
|
||||
"resolved": "https://registry.npmjs.org/tsscmp/-/tsscmp-1.0.6.tgz",
|
||||
"integrity": "sha512-LxhtAkPDTkVCMQjt2h6eBVY28KCjikZqZfMcC15YBeNjkgUpdCfBu5HoiOTDu86v6smE8yOjyEktJ8hlbANHQA=="
|
||||
},
|
||||
"type-fest": {
|
||||
"version": "0.8.1",
|
||||
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",
|
||||
|
@ -1468,6 +1679,11 @@
|
|||
"is-typedarray": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"uid2": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz",
|
||||
"integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
|
||||
},
|
||||
"undefsafe": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.3.tgz",
|
||||
|
|
|
@ -11,8 +11,12 @@
|
|||
"dependencies": {
|
||||
"bcryptjs": "^2.4.3",
|
||||
"body-parser": "^1.19.0",
|
||||
"cookie-session": "^1.4.0",
|
||||
"express": "^4.17.1",
|
||||
"mongoose": "^5.12.2"
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mongoose": "^5.12.2",
|
||||
"passport": "^0.4.1",
|
||||
"passport-google-oauth": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.7"
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
const passport = require('passport') ;
|
||||
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
|
||||
|
||||
|
||||
const User = require('./models/User') ;
|
||||
|
||||
passport.serializeUser(function(user, done) {
|
||||
done(null, user);
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(user, done) {
|
||||
//User.findById(id, function(err, user) {
|
||||
done(null, user);
|
||||
//});
|
||||
});
|
||||
|
||||
passport.use(new GoogleStrategy({
|
||||
clientID: '1026548376782-5p5tjck8ffhan9l1ajhv6orr87dfkrrf.apps.googleusercontent.com',
|
||||
clientSecret: 'I0L_L2XY62MjJBhLJB3eyKly',
|
||||
callbackURL: "http://localhost:3000/auth/google/callback"
|
||||
},
|
||||
async function(accessToken, refreshToken, profile, done) {
|
||||
//User.findOrCreate({ googleId: profile.id }, function (err, user) {
|
||||
|
||||
//return done(null, profile);
|
||||
//});
|
||||
try
|
||||
{
|
||||
let user = await User.findOne({googleId : profile.id}) ;
|
||||
if(user)
|
||||
{
|
||||
return done(null, profile);
|
||||
}
|
||||
else
|
||||
{
|
||||
user = new User({
|
||||
googleId : profile.id ,
|
||||
firstName : profile.name.givenName,
|
||||
lastName : profile.name.givenName ,
|
||||
email : profile._json.email ,
|
||||
}) ;
|
||||
await user.save() ;
|
||||
return done(null, profile);
|
||||
}
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
return done(err, profile);
|
||||
}
|
||||
|
||||
}
|
||||
));
|
|
@ -1,8 +1,10 @@
|
|||
const express = require('express') ;
|
||||
const authController = require('../controllers/auth') ;
|
||||
|
||||
const isAuth = require('../middleware/requirelogin') ;
|
||||
const router = express.Router() ;
|
||||
|
||||
router.get('/protected' ,isAuth,authController.checkProtected) ;
|
||||
|
||||
router.post('/signup' , authController.postSignup) ;
|
||||
|
||||
router.post('/signin' , authController.postSignin) ;
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
const express = require('express') ;
|
||||
const profileRoute = require('../controllers/profile') ;
|
||||
const authMiddleware = require('../middleware/requirelogin') ;
|
||||
|
||||
const router = express.Router() ;
|
||||
|
||||
router.get('/getProfile' , authMiddleware , profileRoute.getProfile) ;
|
||||
|
||||
router.post('/postProfile' , authMiddleware , profileRoute.postProfile) ;
|
||||
|
||||
module.exports = router ;
|
Loading…
Reference in New Issue