-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
125 lines (106 loc) · 4.33 KB
/
index.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
GitHubStrategy = require('passport-github').Strategy
FacebookStrategy = require('passport-facebook').Strategy
TwitterStrategy = require('passport-twitter').Strategy
module.exports =
initialize: (done) ->
passport = require "#{@app.path}/app/lib/passport"
env = require "#{@app.path}/app/env"
app = @app
app.addRoute 'all', '/accounts', 'members-area-passport#passport#accounts'
app.addRoute 'all', '/settings/passport', 'members-area-passport#passport#settings'
@hook 'navigation_items', @modifyNavigationItems.bind(this)
@hook 'render-session-login', @modifyLoginPage.bind(this)
socialProvider = (socialProvider, req, profile, done) ->
req.models.UserLinked.find()
.where(type:socialProvider,identifier:String(profile.id))
.first (err, userLinked) ->
if userLinked?
userLinked.getUser done
else if req.user
data =
type: socialProvider
identifier: String(profile.id)
user_id: req.user.id
req.models.UserLinked.create data, (err, userLinked) ->
userLinked.getUser done
else
# XXX: Better error message
return done new Error("Please log in to your account first and then connect it with this service.")
loggedin = -> (req, res, next) ->
res.redirect "/"
###*
* GitHub Auth
###
supportedProviders = @supportedProviders()
settings = @get()
if supportedProviders.github
passport.use new GitHubStrategy(
clientID: settings.GITHUB_APP_ID
clientSecret: settings.GITHUB_SECRET
callbackURL: env.SERVER_ADDRESS + '/auth/github/callback'
passReqToCallback: true
, (req, accessToken, refreshToken, profile, done) ->
socialProvider('github', req, profile, done)
)
app.get '/auth/github', passport.authenticate('github')
app.get '/auth/github/callback', passport.authenticate('github'), loggedin()
###*
* Facebook Auth
###
if supportedProviders.facebook
passport.use new FacebookStrategy(
clientID: settings.FACEBOOK_APP_ID
clientSecret: settings.FACEBOOK_SECRET
callbackURL: env.SERVER_ADDRESS + '/auth/facebook/callback'
passReqToCallback: true
, (req, accessToken, refreshToken, profile, done) ->
socialProvider('facebook', req, profile, done)
)
app.get '/auth/facebook', passport.authenticate('facebook')
app.get '/auth/facebook/callback', passport.authenticate('facebook'), loggedin()
###*
* Twitter Auth
###
if supportedProviders.twitter
passport.use new TwitterStrategy(
consumerKey: settings.TWITTER_APP_ID
consumerSecret: settings.TWITTER_SECRET
callbackURL: env.SERVER_ADDRESS + "/auth/twitter/callback"
passReqToCallback: true
, (req, token, tokenSecret, profile, done) ->
socialProvider('twitter', req, profile, done)
)
app.get '/auth/twitter', passport.authenticate('twitter')
app.get '/auth/twitter/callback', passport.authenticate('twitter'), loggedin()
done()
supportedProviders: ->
settings = @get()
providers =
github: settings.GITHUB_APP_ID and settings.GITHUB_SECRET
facebook: settings.FACEBOOK_APP_ID and settings.FACEBOOK_SECRET
twitter: settings.TWITTER_APP_ID and settings.TWITTER_SECRET
return providers
modifyLoginPage: (options, done) ->
{controller, $} = options
supportedProviders = @supportedProviders()
providers = []
providers.push "<a class='register' href='/auth/facebook'>Facebook</a>" if supportedProviders.facebook
providers.push "<a class='register' href='/auth/github'>GitHub</a>" if supportedProviders.github
providers.push "<a class='register' href='/auth/twitter'>Twitter</a>" if supportedProviders.twitter
if providers.length
htmlToAdd = "<span>With: </span><br />#{providers.join(" | ")}<br /><br />or:<br /><br />"
$(".login-form h2").after htmlToAdd
done()
modifyNavigationItems: ({addItem}) ->
addItem 'user',
title: 'Accounts'
id: 'members-area-passport-passport-accounts'
href: '/accounts'
priority: 20
addItem 'settings',
title: 'Social login'
id: 'members-area-passport-passport-settings'
href: '/settings/passport'
priority: 100
permissions: ['configure_passport']
return