This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
102 lines (88 loc) · 2.64 KB
/
app.js
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
"use strict";
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var debug = require('debug')('login');
var passport = require('passport');
var session = require('express-session');
var AmebameStrategy = require('passport-amebame').Strategy;
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(session({
secret: 'secretkey',
resave: false,
saveUninitialized: false,
}));
app.use(passport.initialize());
app.use(passport.session());
app.set('port', process.env.PORT || 3001);
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
var clientId = 'YOUR APPLICATION CLIENT ID';
var clientSecret = 'YOUR APPLICATION CLIENT SECRET';
var scope = 'profile,application,connection,photo,coin';
passport.use(new AmebameStrategy({
siteURL: "http://localhost/",
clientID: clientId,
clientSecret: clientSecret,
scope: scope,
authOrigin: 'https://sb.dauth.user.ameba.jp',
profileOrigin: 'https//sb-profile-api.ameba.jp',
},
function(accessToken, refreshToken, params, profile, done) {
process.nextTick(function() {
done(null, profile);
});
}
));
app.get('/login', passport.authenticate('amebame'));
app.get('/login_callback',
function(req, res, next) {
passport.authenticate('amebame', function(err, user) {
if (err) {
return res.redirect('/?ng=0');
}
if (!user) {
return res.redirect('/?ok=1');
}
req.login(user, {session: true}, function() {
res.redirect('/?ok=2');
});
});
}
);
app.use('/', function(req, res) {
var status = 'login';
if (req.isAuthenticated()) {
status = 'logined';
}
var html = '<ul>' +
'<li><a href="/login">' + status + '</a></li>' +
'<li><a href="/login?provider_id=twitter">login provider_id=twitter</a></li>' +
'<li><a href="/login?frm_id=frmid">login frm_id</a></li>' +
'<li><a href="/login?frm_id=frmid2&state=hello">login state</a></li>' +
'</ul>';
res.send(html);
});
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
module.exports = app;