Skip to content

auth.login

Samuel S. Donovan edited this page Feb 2, 2022 · 7 revisions

auth.login utilizes auth.users and auth.sessions to create a cookie and a session token, which together are used to manage session.

auth.login.endpoint provides a function with the signature function(req, res) that can be used to easily create a login endpoint.

Method

NOTE: As this method sets a cookie, it is imperative that it be called before any body is sent otherwise an error will be encountered statingError: Can't render headers after they are sent to the client..

Endpoint

auth.login.endpoint provides an optional ready-to-use endpoint for logging users in. As some functionality may want to be expanded

auth.login.endpoint requires that req.body be defined with req.body.username and req.body.password as strings. This can be accomplished using express.json(). Various response codes are given depending on if the login was successful or not.

Example



const express = require('express');
const app = express();

app.use(express.json()); //Needed

const cookieParser = require('cookie-parser');
app.use(cookieParser("secret"));

const auth = require('express-auth');
app.use(auth);

app.post("/login", auth.login.endpoint);

Expected Body


{
    "username": "string",
    "password": "string
}

Response codes

200 Login successful
401 Login failed

Clone this wiki locally