-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] GitHub authentication #224
Merged
Merged
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7d475ca
start implementing sessions
Rich-Harris bc80320
tidy up
Rich-Harris c1536aa
factor out <SplitPane>
Rich-Harris 99f6214
load codemirror inside the codemirror component
Rich-Harris b688798
extract ModuleEditor
Rich-Harris 91ab11c
remove unused CSS
Rich-Harris f7d507c
minor tidy up
Rich-Harris 3f1d53c
remove unused vars
Rich-Harris 1d074c8
do compilation in a worker
Rich-Harris 19eb665
fix error locations
Rich-Harris 665b67c
remove some unused stuff
Rich-Harris 24518e4
move examples into gists, for unified approach to loading
Rich-Harris 7123bb8
better dropdown
Rich-Harris 73c6942
doh
Rich-Harris a84c0ad
tidy up
Rich-Harris 004a372
move some things around, remove some unused stuff
Rich-Harris f6d1771
fall back to username if displayName is not supplied
Rich-Harris 1a0f034
remove obsolete reference to store
Rich-Harris 89ca47b
only null out deleted components, preserve README etc
Rich-Harris 7b0442b
typo
Rich-Harris d8d80e6
maybe this works?
Rich-Harris 78a56a9
update URL when necessary
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ assets/svelte-app.zip | |
credentials.json | ||
app/manifest | ||
build | ||
yarn-error.log | ||
yarn-error.log | ||
.env | ||
.sessions |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,107 @@ | ||
import dotenv from 'dotenv'; | ||
import express from 'express'; | ||
import compression from 'compression'; | ||
import session from 'express-session'; | ||
import passport from 'passport'; | ||
import { Strategy } from 'passport-github'; | ||
import sessionFileStore from 'session-file-store'; | ||
import sapper from 'sapper'; | ||
import serve from 'serve-static'; | ||
import devalue from 'devalue'; | ||
import { Store } from 'svelte/store.js'; | ||
import { routes } from './manifest/server.js'; | ||
|
||
dotenv.config(); | ||
|
||
const FileStore = sessionFileStore(session); | ||
|
||
passport.use(new Strategy({ | ||
clientID: process.env.GITHUB_CLIENT_ID, | ||
clientSecret: process.env.GITHUB_CLIENT_SECRET, | ||
callbackURL: `http://${process.env.ORIGIN}/auth/callback` | ||
}, (accessToken, refreshToken, profile, callback) => { | ||
console.log(profile); | ||
|
||
return callback(null, { | ||
id: profile.id, | ||
username: profile.username, | ||
displayName: profile.displayName, | ||
photo: profile.photos && profile.photos[0] && profile.photos[0].value, | ||
|
||
accessToken | ||
}); | ||
})); | ||
|
||
passport.serializeUser((user, cb) => { | ||
cb(null, user); | ||
}); | ||
|
||
passport.deserializeUser((obj, cb) => { | ||
cb(null, obj); | ||
}); | ||
|
||
express() | ||
.use(compression({ threshold: 0 })) | ||
|
||
.use(session({ | ||
secret: 'svelte', | ||
resave: true, | ||
saveUninitialized: true, | ||
cookie: { | ||
maxAge: 31536000 | ||
}, | ||
store: new FileStore({ | ||
path: process.env.NOW ? `/tmp/sessions` : `.sessions` | ||
}) | ||
})) | ||
|
||
.use(passport.initialize()) | ||
.use(passport.session()) | ||
|
||
.get('/auth/login', (req, res, next) => { | ||
const { returnTo } = req.query; | ||
req.session.returnTo = returnTo ? decodeURIComponent(returnTo) : '/'; | ||
next(); | ||
}, passport.authenticate('github', { scope: ['gist', 'read:user'] })) | ||
|
||
.post('/auth/logout', (req, res) => { | ||
req.logout(); | ||
res.end('ok'); | ||
}) | ||
|
||
.get('/auth/callback', passport.authenticate('github', { failureRedirect: '/auth/error' }), (req, res) => { | ||
const user = req.session.passport && req.session.passport.user; | ||
if (user) delete user.accessToken; | ||
|
||
res.end(` | ||
<script> | ||
window.parent.postMessage({ | ||
user: ${devalue(user)} | ||
}, window.location.origin); | ||
</script> | ||
`); | ||
}) | ||
|
||
.use( | ||
compression({ threshold: 0 }), | ||
serve('assets'), | ||
sapper({ | ||
routes | ||
routes, | ||
store: req => { | ||
const user = req.session.passport && req.session.passport.user; | ||
|
||
return new Store({ | ||
user: user && { | ||
// strip access token | ||
id: user.id, | ||
name: user.name, | ||
displayName: user.displayName, | ||
photo: user.photo | ||
}, | ||
guide_contents: [] | ||
}); | ||
} | ||
}) | ||
) | ||
|
||
.listen(process.env.PORT); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Store } from 'svelte/store.js'; | ||
|
||
class ClientStore extends Store { | ||
logout() { | ||
return fetch(`auth/logout`, { | ||
method: 'POST', | ||
credentials: 'include' | ||
}).then(() => { | ||
this.set({ user: null }); | ||
}); | ||
} | ||
} | ||
|
||
export default new ClientStore(); |
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be
username: user.username
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, you're right — fixed