-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Add passport
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import passport from 'passport'; | ||
import passportLocal from 'passport-local'; | ||
import _ from 'lodash'; | ||
import { User, UserDocument } from '../models/User'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
|
||
const LocalStrategy = passportLocal.Strategy; | ||
|
||
passport.serializeUser<any, any>((user, done) => { | ||
done(undefined, user.id); | ||
}); | ||
|
||
passport.deserializeUser((id, done) => { | ||
User.findById(id, (err, user) => { | ||
done(err, user); | ||
}); | ||
}); | ||
|
||
passport.use( | ||
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => { | ||
User.findOne({ email: email.toLowerCase() }, (err, user: any) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
user.comparePassword(password, (err: Error, isMatch: boolean) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
if (isMatch) { | ||
return done(undefined, user); | ||
} | ||
return done(undefined, false, { message: 'Invalid email or password' }); | ||
}); | ||
}); | ||
}) | ||
); | ||
|
||
export const isAuthenticated = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
if (req.isAuthenticated()) { | ||
return next(); | ||
} | ||
res.redirect('/login'); | ||
}; | ||
|
||
export const isAuthorized = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const provider = req.path.split('/').slice(-1)[0]; | ||
|
||
const user = req.user as UserDocument; | ||
if (_.find(user.tokens, { kind: provider })) { | ||
next(); | ||
} else { | ||
res.redirect(`/auth/${provider}`); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import mongoose from 'mongoose'; | ||
import bcrypt from 'bcrypt-nodejs'; | ||
|
||
export type UserDocument = mongoose.Document & { | ||
email: string; | ||
passowrd: string; | ||
tokens: AuthToken[]; | ||
comparePassword: comparePasswordFunction; | ||
}; | ||
|
||
export interface AuthToken { | ||
accessToken: string; | ||
kind: string; | ||
} | ||
type comparePasswordFunction = ( | ||
candidatePassword: string, | ||
cb: (err: any, isMatch: any) => {} | ||
) => void; | ||
|
||
const userSchema = new mongoose.Schema( | ||
{ | ||
email: { type: String, unique: true }, | ||
passowrd: { type: String }, | ||
profile: { | ||
email: String | ||
} | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
userSchema.pre('save', function save(next) { | ||
const user = this as UserDocument; | ||
if (!user.isModified('password')) { | ||
return next(); | ||
} | ||
bcrypt.genSalt(10, (err, salt) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
bcrypt.hash(user.passowrd, salt, undefined, (err: mongoose.Error, hash) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
user.passowrd = hash; | ||
return next(); | ||
}); | ||
}); | ||
}); | ||
|
||
const comparePassword: comparePasswordFunction = function( | ||
candidatePassword, | ||
cb | ||
) { | ||
bcrypt.compare( | ||
candidatePassword, | ||
this.passowrd, | ||
(err: mongoose.Error, isMatch: Boolean) => { | ||
cb(err, isMatch); | ||
} | ||
); | ||
}; | ||
|
||
userSchema.methods.comparePassword = comparePassword; | ||
|
||
export const User = mongoose.model<UserDocument>('User', userSchema); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import winston from 'winston'; | ||
|
||
const options: winston.LoggerOptions = { | ||
transports: [ | ||
new winston.transports.Console({ | ||
level: process.env.NODE_ENV === 'production' ? 'error' : 'debug' | ||
}), | ||
new winston.transports.File({ filename: 'debug.log', level: 'debug' }) | ||
] | ||
}; | ||
|
||
const logger = winston.createLogger(options); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
logger.debug('Logging initialized at debug level'); | ||
} | ||
|
||
export default logger; |