@aytacworld/express-module-login
This module is ment to be used with @aytacworld/express.
It will add login routes and login services to the application.
Needed modules
For this module you need authDb module to be loaded before loading this module.
The needed modules needs to be loaded before loading login module.
There are already 2 modules for that purpose.
- @aytacworld/express-module-memory uses in-memory database
- @aytacworld/express-module-mongo uses mongodb
- or you can create your own. (follow the database structure below)
Installation
using npm
npm i @aytacworld/express-module-login
using yarn
yarn add @aytacworld/express-module-login
Usage
app.js
const Express = require('@aytacworld/express');
const ExpressMongo = require('@aytacworld/express-module-mongo');
const ExpressLogin = require('@aytacworld/express-module-login');
const myOtherRoutes = require('./routes');
const app = new Express({});
app.module(ExpressMongo, ExpressLogin);
app.route('/', myOtherRoutes);
app.listen();
The following routes will be added.
- GET /login
- POST /login
- GET /login/logout
If you need to check if the user is logged in, you can use the helper module(which is built-in in @aytacworld/express).
routes.js
const path = require('path');
const router = require('express').Router();
// login module is added.
const Router = (config, { helper, db }) => {
helper.addPathToViewEngine(path.resolve(__dirname, 'views'));
router.get('/', async (req, res) => {
res.render('homepage', { user: req.user }); // req.user can be undefined
});
router.get('/profile',
helper.isAuthenticated(), // checks whether the user is filled in or not
async (req, res) => {
res.render('profile', { user: req.user }); // req.user is always filled in
});
return router;
};
module.exports = Router;
Config
{
"EXPRESS_MODULE_LOGIN_DISABLE_VIEW": true, // Boolean, default: undefined, this will don't add default pug files in your project, so you need to create your own login page
"EXPRESS_MODULE_LOGIN_VIEW_NAME": "my-custom-login-view", // String, default: 'aytacworld-express-module-login'
"EXPRESS_MODULE_LOGIN_CSS": "/public/login.css", // String, default: undefined
}
Updating login page
There are 3 ways of updating the login view-page:
-
set
EXPRESS_MODULE_LOGIN_DISABLE_VIEW
to true in your config file and create aaytacworld-express-module-login.pug
in your views folder. -
set
EXPRESS_MODULE_LOGIN_VIEW_NAME
to another view name and create a view with the name you provide. -
set
EXPRESS_MODULE_LOGIN_CSS
to point to your css file, then you can use classes.
This is the login.pug template, so you can find the class you need.
login.pug
h1.module-login-h1 Please sign in
form(method='POST' action='/login').module-login-form
input(type='hidden' name="redirectUrl" value=`${redirectUrl || ''}`)
.module-login-form-control
label.module-login-label Username
input(type='text' placeholder='Username' name='username' required).module-login-input.module-login-input-username
.module-login-form-control
label.module-login-label Password
input(type='password' placeholder='Password' name='password' required).module-login-input.module-login-input-password
.module-login-form-control
button(type='submit').module-login-button Login
Database structure
{
users: {
static findById(id): Promise<user>;
static comparePassword(username, password): Promise<user|false>;
},
}
User model
class User {
id: any;
username: string;
password: string;
}
INFO: don't forget to hash the password before saving in database!
MIT License
Copyright (c) 2019 Adem Aytaç
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.