Skip to content

Commit

Permalink
Merge pull request keystonejs#935 from gautamsi/gautamsi/improve-twit…
Browse files Browse the repository at this point in the history
…ter-login

improve twitter login from keystonejs#164 code.
  • Loading branch information
timleslie authored Mar 28, 2019
2 parents 54ec7a4 + 0a34eb5 commit ec55c9a
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions test-projects/twitter-login/twitter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const TwitterAuthStrategy = require('@keystone-alpha/keystone/auth/Twitter');
const { startAuthedSession } = require('@keystone-alpha/session');
const { startAuthedSession, endAuthedSession } = require('@keystone-alpha/session');
const { appURL, twitterAppKey, twitterAppSecret } = require('./config');

exports.configureTwitterAuth = function(keystone, server) {
Expand Down Expand Up @@ -37,29 +37,58 @@ exports.configureTwitterAuth = function(keystone, server) {
});
});

server.app.get('/api/signout', async (req, res, next) => {
try {
await endAuthedSession(req);
res.json({
success: true,
});
} catch (e) {
next(e);
}
});

// Twitter will redirect the user to this URL after approval.
server.app.get(
'/auth/twitter/callback',
twitterAuth.authenticateMiddleware({
async verified(item, { list }, req, res) {
// You could try and find user by email address here to match users
// if you get the email data back from Twitter, then refer to
// connectItem, for example:
// await keystone.auth.User.twitter.connectItem(req, { item });
// ...

// If we don't have a matching user in our system, force redirect to
// the create step
if (!item) {
return res.redirect('/auth/twitter/details');
return res.redirect('/auth/twitter/create');
}

// Otheriwse create a session based on the user we have already
await startAuthedSession(req, { item, list });
// Redirect on sign in
res.redirect('/api/session');
},
failedVerification(error, req, res) {
console.log('🤔 Failed to verify Twitter login creds');
console.log('Failed to verify Twitter login creds');
res.redirect('/');
},
})
);

// Sample page to collect a name, submits to the completion step
server.app.get('/auth/twitter/details', (req, res) => {
// Sample page to collect a name, submits to the completion step which will
// create a user
server.app.get('/auth/twitter/create', (req, res) => {
// Redirect if we're already signed in
if (req.user) {
return res.redirect('/api/session');
}
// If we don't have a keystoneTwitterSessionId at this point, the form
// submission will fail, so fail out to the first step
if (!req.session.keystoneTwitterSessionId) {
return res.redirect('/auth/twitter');
}

res.send(`
<form action="/auth/twitter/complete" method="post">
Expand All @@ -74,10 +103,12 @@ exports.configureTwitterAuth = function(keystone, server) {
'/auth/twitter/complete',
server.express.urlencoded({ extended: true }),
async (req, res, next) => {
// Redirect if we're already signed in
if (req.user) {
return res.redirect('/api/session');
}

// Create a new User
try {
const list = keystone.getListByKey('User');

Expand Down

0 comments on commit ec55c9a

Please sign in to comment.