Skip to main content

Local

Options

Option nameDescriptionDefault valueRequired?
usernameFieldName of the field that represents the username in the body of the request'username'No
passwordFieldName of the field that represents the password in the body of the request'password'No

Handler

The Local Provider passes three parameters to the Handler

  • username: Username sent in the request
  • password: Password sent in the request
  • req: The request object (in case there is other data that is needed, like profile picture, country of origin, email, etc.)

Errors

The predefined errors of this provider are:

  • IncorrectCredentials

Examples

Example #1

Create a Local Provider

import crypto from 'crypto'
import gatekeeper from 'gatekeeper-authentication'
import LocalProvider, { IncorrectCredentials } from 'gatekeeper-authentication/providers/local'

const LocalAuth = new LocalProvider((username, password, req) => {
const user = db.query('SELECT * FROM users WHERE username = ? LIMIT 1', username);
if (user == null)
throw new IncorrectCredentials();

const hashedPassword = crypto.createHash('sha256').update(password).digest('hex')
if (hashedPassword !== user.password)
throw new IncorrectCredentials();

return user;
}, (error, req, res, next) => {
if (error instanceof IncorrectCredentials)
return res.json({ error: 'Your username or password are incorrect' });

return next(error);
});

Login with a Local Provider

// You can use it in both GET...
router.get(
'/auth/local',
gatekeeper.authenticateWithProvider(LocalAuth), (req, res) => {
return res.redirect('/profile');
}
);
// ...and POST routes
router.post(
'/auth/local',
gatekeeper.authenticateWithProvider(LocalAuth), (req, res) => {
return res.redirect('/profile');
}
);
``