1.0.0 • Published 5 years ago
@akeraio/passport v1.0.0
passport-akera
Passport authentication strategy against Akera.io application server. This module is a Passport strategy wrapper for @akera/api
Install
npm install @akeraio/passportUsage
Configure strategy
Typescript
import {Strategy as AkeraStrategy} from "@akeraio/passport";
import * as passport from "passport";
passport.use(new AkeraStrategy({
server: {
host: 'localhost',
port: 8383,
useSSL: true
},
...
}));Javascript
const AkeraStrategy = require('@akeraio/passport').Strategy;
passport.use(new AkeraStrategy({
server: {
host: 'localhost',
port: 8383,
useSSL: true
},
...
}));server: Akera.io settings. These are passed directly to @akera/api connect method. See its documentation for all available options.host: Akera.io application server host name or ip address, e.g.localhostport: Akera.io application server port number, e.g.8383useSSL: Akera.io application server SSL connection flag
usernameField: Field name where the user name is found, defaults to usernamepasswordField: Field name where the password is found, defaults to passwordpassReqToCallback: Whentrue,reqis the first argument to the verify callback (default:false):passport.use(new AkeraStrategy(..., function(req, user, done) { ... done(null, user); } ));
Note: you can pass a function instead of an object as options, see the example below
Authenticate requests
Use passport.authenticate(), specifying the 'akera' strategy, to authenticate requests.
authenticate() options
In addition to default authentication options the following flash message options are available for passport.authenticate():
badRequestMessage: missing username/password (default: 'Missing credentials')invalidCredentials:InvalidCredentialsError(default: error returned by akera-api connect)
Express example
Typescript
import express from "express";
import passport from "passport";
import {json, urlencoded} from "body-parser";
import {Strategy as AkeraStrategy} from "@akeraio/passport";
const OPTS = {
server: {
host: 'localhost',
port: '8383',
useSSL: true
}
};
const app = express();
passport.use(new AkeraStrategy(OPTS));
app.use(json());
app.use(urlencoded({extended: false}));
app.use(passport.initialize());
app.post('/login', passport.authenticate('akera', {session: false}), function(req, res) {
res.send({status: 'ok'});
});
app.listen(8080);Javascript
const express = require('express');
const passport = require('passport');
const bodyParser = require('body-parser');
const AkeraStrategy = require('passport-akera').Strategy;
const OPTS = {
server: {
host: 'localhost',
port: '8383',
useSSL: true
}
};
const app = express();
passport.use(new AkeraStrategy(OPTS));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(passport.initialize());
app.post('/login', passport.authenticate('akera', {session: false}), function(req, res) {
res.send({status: 'ok'});
});
app.listen(8080);License
MIT
1.0.0
5 years ago