Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from openameba/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
herablog authored Oct 5, 2022
2 parents 1c2a0d2 + ea6ed3e commit 8baba79
Show file tree
Hide file tree
Showing 16 changed files with 1,037 additions and 313 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
example/
test/
.npmignore
.github/
65 changes: 20 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,27 @@
$ npm install passport-amebame --save
```

## 使い方

### Configure Strategy

```
passport.use(new AmebameStrategy({
siteURL: "http://localhost/",
clientID: AMEBAME_CLIENT_ID,
clientSecret: AMEBAME_CLIENT_SECRET,
scope: scope,
sandbox: true
},
function(accessToken, refreshToken, params, profile, done) {
User.findOrCreate({amebameId: profile.id, as_id: params.as_id}, function(err, user) {
return done(err, user);
});
}
));
## Usage

### Strategy Configuration

```JavaScript
const Amebame = require('passport-amebame');

passport.use(new Amebame.Strategy({
clientID: 'YOUR_CLIENT_ID', // Required
clientSecret: 'YOUR_CLIENT_SECRET', // Required
scope: '', // Optional
authOrigin: '', // Optional
profileOrigin: '', // Optional
}, function(accessToken, refreshToken, params, profile, done) {
User.findOrCreate({ id: profile.id, profile }, function(err, user) {
if (err) { return done(err); }
done(null, user);
});
}));
```

### Authenticate Request


```
app.get('/auth/amebame',
passport.authenticate('amebame'));
// 認可が完了していない場合,自分でredirect先を設定する必要がある
var registerRedirect = AmebameService.redirectRegisterPage({clientID: clientId, registerCallbackURL: "http://localhost"})
app.get('/auth/amebame/callback',
function(req, res, next) {
passport.authenticate('amebame', function(err, user, info) {
if (err && err.name === 'AmebameNotRegisteredError') {
registerRedirect(req, res);
} else {
if (err) {
res.redirect("/error");
} else {
req.login(user, {session: true}, function() {
res.redirect('/');
});
}
}
})(req, res, next);
});
```

You can see it in [the example application](/example/login/app.js).
49 changes: 19 additions & 30 deletions example/login/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var debug = require('debug')('login');
var passport = require('passport');
var session = require('express-session');
var AmebameStrategy = require('passport-amebame').Strategy;
var AmebameService = require('passport-amebame').Service;

var app = express();

Expand All @@ -22,7 +21,9 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(session({
secret: 'secretkey'
secret: 'secretkey',
resave: false,
saveUninitialized: false,
}));
app.use(passport.initialize());
app.use(passport.session());
Expand All @@ -45,7 +46,8 @@ passport.use(new AmebameStrategy({
clientID: clientId,
clientSecret: clientSecret,
scope: scope,
sandbox: true
authOrigin: 'https://sb.dauth.user.ameba.jp',
profileOrigin: 'https//sb-profile-api.ameba.jp',
},
function(accessToken, refreshToken, params, profile, done) {
process.nextTick(function() {
Expand All @@ -55,35 +57,22 @@ passport.use(new AmebameStrategy({
));

app.get('/login', passport.authenticate('amebame'));
app.get('/redirect_test', AmebameService.redirectRegisterPage({
clientID: clientId,
registerCallbackURL: "http://localhost",
sandbox: true
}));

app.get('/login_callback',
function(req, res, next) { // registerしていないときは 登録画面に遷移させる
passport.authenticate('amebame', function(err, user) {
if (err) {
if (err.name === 'AmebameNotRegisteredError') {
AmebameService.redirectRegisterPage({
clientID: clientId,
registerCallbackURL: "http://localhost",
sandbox: true
})(req, res);
} else {
res.redirect('/?ng=0');
}
} else {
if (!user) {
return res.redirect('/?ok=1');
}
req.login(user, {session: true}, function() {
res.redirect('/?ok=2');
});
}
})(req, res, next);
});
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';
Expand Down
46 changes: 0 additions & 46 deletions lib/passport-amebame/error/amebame_not_registered_error.js

This file was deleted.

86 changes: 86 additions & 0 deletions lib/passport-amebame/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import passport = require('passport');
import express = require('express');

type ProfilePrivacy = 'anonymous' | 'everyone' | 'onlyMe';

type ProfileJson = {
id: {
amebaId: string,
asId: string,
},
time: {
create: string,
update: string,
},
nickname: {
value: string,
censored: boolean,
},
gender: {
value: string,
privacy: ProfilePrivacy
},
birthday: {
value: string,
privacy: ProfilePrivacy,
changed: boolean,
},
profileImage: {
originUrl: string,
thumbnailUrl: string,
censored: boolean,
exists: boolean,
meta: {
amebaOriginWidth: number,
amebaOriginHeight: number,
amebaThumbnailWidth: number,
amebaThumbnailHeight: number,
},
}
};

interface Profile extends passport.Profile {
id: string;
displayName: string;
imageUrl: string;
gender?: string;
birthday?: string;

_raw: string;
_json: ProfileJson;
}

interface IStrategyOption {
clientID: string;
clientSecret: string;
scope?: string;
authOrigin?: string;
profileOrigin?: string;
}

interface IVerifyParams {
access_token: string;
as_id: string;
expires_in: string;
}

interface IAuthorizationParams {
frm_id?: string;
ameba_frmId?: string;
provider_id?: string;
state?: string;
redirect_uri?: string;
force_provide?: string;
response_type?: string;
display?: string;
}

export class Strategy extends passport.Strategy {
constructor(options: IStrategyOption,
verify: (accessToken: string, refreshToken: string, params: IVerifyParams, profile: Profile, done: (error: Error | null, user?: Express.User) => void) => void);

name: string;
authenticate(req: express.Request, options?: Object): void;
authorizationParams(options: express.Request): IAuthorizationParams;
userProfile(accessToken: string, done: (error: Error | null, user?: { provider: 'amebame' } & Profile) => void): void;
}
6 changes: 0 additions & 6 deletions lib/passport-amebame/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
*/
var Strategy = require('./strategy');

/**
* Service Module
*/
var Service = require('./service');

/**
* Framework version.
*/
Expand All @@ -19,4 +14,3 @@ require('pkginfo')(module, 'version');
* Expose constructors
*/
exports.Strategy = Strategy;
exports.Service = Service;
36 changes: 0 additions & 36 deletions lib/passport-amebame/service.js

This file was deleted.

Loading

0 comments on commit 8baba79

Please sign in to comment.