#57 Finished google and password login endpoints for android
parent
76c4e7696e
commit
c2a3b14a90
|
@ -31,6 +31,7 @@ module.exports = (passport)=>{
|
|||
passwordField: 'password',
|
||||
passReqToCallback: true
|
||||
}, (req,email,password,done)=>{
|
||||
//console.log(`Perfoming local login for ${email}`);
|
||||
User.findOne({'email':email})
|
||||
.then( (user)=>{
|
||||
|
||||
|
@ -72,13 +73,13 @@ module.exports = (passport)=>{
|
|||
|
||||
// Social login
|
||||
function socialLogin(req, service, profileId, done) {
|
||||
// console.log(`socialLogin() called`);
|
||||
//console.log(`socialLogin() called`);
|
||||
let query = {};
|
||||
query['auth.'+service] = profileId;
|
||||
|
||||
// Intent to log in
|
||||
if (!req.user) {
|
||||
// console.log(`Logging in with ${service}...`);
|
||||
//console.log(`Logging in with ${service}...`);
|
||||
User.findOne(query)
|
||||
.then( (user)=>{
|
||||
|
||||
|
@ -123,7 +124,7 @@ module.exports = (passport)=>{
|
|||
|
||||
// No googleId either
|
||||
else {
|
||||
// console.log(`Couldn't find ${service} user.`);
|
||||
//console.log(`Couldn't find ${service} user.`);
|
||||
req.session.flashType = 'warning';
|
||||
req.session.flashMessage = `There's no user for that ${service} account. `;
|
||||
return done();
|
||||
|
@ -132,7 +133,7 @@ module.exports = (passport)=>{
|
|||
|
||||
// Successfull social login
|
||||
else {
|
||||
// console.log(`Found user: ${user}`);
|
||||
//console.log(`Found user: ${user}`);
|
||||
req.session.flashType = 'success';
|
||||
req.session.flashMessage = "You have been logged in.";
|
||||
return done(null, user);
|
||||
|
@ -147,7 +148,7 @@ module.exports = (passport)=>{
|
|||
|
||||
// Intent to connect account
|
||||
else {
|
||||
// console.log(`Attempting to connect ${service} account...`);
|
||||
//console.log(`Attempting to connect ${service} account...`);
|
||||
|
||||
// Check for unique profileId
|
||||
User.findOne(query)
|
||||
|
@ -155,7 +156,7 @@ module.exports = (passport)=>{
|
|||
|
||||
// Social account already in use
|
||||
if (existingUser) {
|
||||
// console.log(`${service} account already in use.`);
|
||||
//console.log(`${service} account already in use.`);
|
||||
req.session.flashType = 'warning';
|
||||
req.session.flashMessage = `Another user is already connected to that ${service} account. `;
|
||||
return done();
|
||||
|
@ -163,7 +164,7 @@ module.exports = (passport)=>{
|
|||
|
||||
// Connect to account
|
||||
else {
|
||||
// console.log(`Connecting ${service} account.`);
|
||||
//console.log(`Connecting ${service} account.`);
|
||||
req.user.auth[service] = profileId;
|
||||
req.user.save()
|
||||
.then( ()=>{
|
||||
|
|
|
@ -16,14 +16,14 @@ module.exports = (app, passport) => {
|
|||
failureFlash: true
|
||||
},
|
||||
loginCallback = (req,res)=>{
|
||||
// console.log(`Login callback called... redirecting to ${req.session.next}`);
|
||||
//console.log(`Login callback called... redirecting to ${req.session.next}`);
|
||||
req.flash(req.session.flashType,req.session.flashMessage);
|
||||
req.session.flashType = undefined;
|
||||
req.session.flashMessage = undefined;
|
||||
res.redirect( req.session.next || '/map' );
|
||||
},
|
||||
appLoginCallback = (req,res,next)=>{
|
||||
// console.log('appLoginCallback called.');
|
||||
//console.log('appLoginCallback called.');
|
||||
if (req.user){ res.send(req.user); }
|
||||
else {
|
||||
let err = new Error("Unauthorized");
|
||||
|
@ -240,12 +240,12 @@ module.exports = (app, passport) => {
|
|||
} );
|
||||
|
||||
// Android
|
||||
app.get('/login/app/', passport.authenticate('local'), appLoginCallback);
|
||||
app.post('/login/app', passport.authenticate('local'), appLoginCallback);
|
||||
|
||||
// Token-based (android social)
|
||||
app.get(['/login/app/google','/auth/google/idtoken'], passport.authenticate('google-token'), appLoginCallback);
|
||||
app.get('/login/app/facebook', passport.authenticate('facebook-token'), appLoginCallback);
|
||||
app.get('/login/app/twitter', passport.authenticate('twitter-token'), appLoginCallback);
|
||||
// app.get('/login/app/facebook', passport.authenticate('facebook-token'), appLoginCallback);
|
||||
// app.get('/login/app/twitter', passport.authenticate('twitter-token'), appLoginCallback);
|
||||
|
||||
// Social
|
||||
app.get('/login/:service', (req,res,next)=>{
|
||||
|
@ -254,19 +254,19 @@ module.exports = (app, passport) => {
|
|||
|
||||
// Social login
|
||||
if (!req.user) {
|
||||
// console.log(`Attempting to login with ${service} with params: ${JSON.stringify(sendParams)}...`);
|
||||
//console.log(`Attempting to login with ${service} with params: ${JSON.stringify(sendParams)}...`);
|
||||
passport.authenticate(service, sendParams)(req,res,next);
|
||||
}
|
||||
|
||||
// Connect social account
|
||||
else if (!req.user.auth[service]) {
|
||||
// console.log(`Attempting to connect ${service} account...`);
|
||||
//console.log(`Attempting to connect ${service} account...`);
|
||||
passport.authorize(service, sendParams)(req,res,next);
|
||||
}
|
||||
|
||||
// Disconnect social account
|
||||
else {
|
||||
// console.log(`Attempting to disconnect ${service} account...`);
|
||||
//console.log(`Attempting to disconnect ${service} account...`);
|
||||
req.user.auth[service] = undefined;
|
||||
req.user.save()
|
||||
.then(()=>{
|
||||
|
|
Loading…
Reference in New Issue