Last active
November 5, 2017 21:32
-
-
Save kndt84/62f4b3ea9547773a26dd37e6c0cc788d to your computer and use it in GitHub Desktop.
Express + Passport + Cognito でサーバーサイドのユーザー認証を手軽に実現 ref: http://qiita.com/kndt84/items/246d724a921d22c1eb7d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var passport = require('passport'); | |
// passportモジュールをLoad | |
require('./passport')(app); | |
// session用のmiddlewaresを有効化 | |
app.use(passport.initialize()); | |
app.use(passport.session()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CognitoStrategy = require('passport-cognito') | |
module.exports = new CognitoStrategy({ | |
userPoolId: 'ap-northeast-1_eSjqLfqKc', | |
clientId: 'vtvg02tr21zmxvspyvawtv09b', | |
region: 'ap-northeast-1' | |
}, | |
function(accessToken, idToken, refreshToken, user, cb) { | |
process.nextTick(function() { | |
... | |
cb(null, user); | |
}); | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install passport-cognito |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax({ | |
type: 'POST', | |
url: '/auth/cognito', | |
data: { username: username, password: password } | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/auth/cognito', | |
passport.authenticate('cognito', { | |
successRedirect: '/', | |
failureRedirect: '/login' | |
})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(){ | |
var passport = require('passport'); | |
// sessionにユーザー(のキー)情報を格納する処理 | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
// sessionからユーザー情報を復元する処理 | |
passport.deserializeUser(function(obj, done) { | |
done(null, obj); | |
}); | |
// 利用するstrategyを設定 | |
passport.use(require('./passport/cognito')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment