RAuth library provides a simple way for using Authorization and Authentication via JWT encapsulating their main methods. Allows to handle multiple sessions ensuring trust between an application and its users.
$ npm install rauth
import 'rauth/engines/SQLiteEngine';
import { SessionControl } from 'rauth/session/SessionControl';
const sessionControl = new SessionControl({ engineConnectionStore: 'SQLite' });
This handler allows create the session
// Handler to GET /authorize?grant_type=basic
// Here is your method to validate the credentials
const session = await sessionControl.createSession(username);
res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify(session), 'utf8');
const token = query.token;
const session = await sessionControl.verify(token);
This handler allows refresh the session.
// Handler to GET /authorize?grant_type=refresh_token&refresh_token=...
const refreshToken = query.refresh_token;
const session = await sessionControl.refreshSession(refreshToken);
res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify(session), 'utf8');
This handler allows revoke a refresh token.
// Handler to GET /logout
await sessionControl.revokeSession(session);
res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify(session), 'utf8');
await sessionControl.revokeAllSessions(session)
const sessions = await sessionControl.getAllSessions(session);
sessionControl.on('create-session', callback(){});
Events list and arguments:
create-session
: Event emitted after of created the object o row in your Storage.- Args:
{ register: Register }
regiter
(Register
): Register inserted in your Storage.
- Args:
refresh-session
: Event emitted after of to refresh the object o row in your Storage.- Args:
{ register: Register }
regiter
(Register
): Register inserted in your Storage.
- Args:
The engines help control the session storage. Currently, RAuth provides following engines:
- Mongoose
rauth/engines/MongooseEngine
(Sample) - SQLite
rauth/engines/SQLiteEngine
(Requiressqlite
installed) - Memory
rauth/engines/MemoryEngine
- TypeORM
rauth/engines/TypeormEngine
(Sample Requirestypeorm
installed)
Mongoose:
import 'rauth/engines/MongooseEngine';
export const sessionControl = new SessionControl({
connectionStore: new ConnectionStore('Mongoose', { model: SessionModel }),
});
Memory:
import 'rauth/engines/MemoryEngine';
export const sessionControl = new SessionControl({
engineConnectionStore: 'Memory',
});
SQLite:
import 'rauth/engines/SQLiteEngine';
export const sessionControl = new SessionControl({
engineConnectionStore: 'SQLite',
});
// Or
import 'rauth/engines/SQLiteEngine';
export const sessionControl = new SessionControl({
connectionStore: new ConnectionStore('SQLite', {
filename: `${__dirname}/db.sqlite`,
table: 'sessions',
}),
});
Typeorm:
import '../engines/TypeormEngine';
export const sessionControl = new SessionControl({
connectionStore: new ConnectionStore('Typeorm', { entity: Session }),
});