express-account-block
A simple to use module for Express that handles authentification (signup and login), sessions and provide an account page you can place in your app.
Requires JQuery and Bootstrap 4 (JS + CSS) on the client side.
Features
Who uses it?
|
|
|
|
|
|
Nucleus | ElioPay | Backery | Anygoal | Lttrfeed | Musli.io |
👋 Want to be listed there? Contact me.
Usage
npm i express-account-block
Then in your express code
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const mongoURI = process.env.MONGO_URL || 'mongodb://localhost:27017/myappdb'
mongoose.connect(mongoURI, { useNewUrlParser: true })
const db = require('./helpers/db')
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
require('express-account-block')(app, {
siteName: 'My Web App',
siteUrl: 'example.com',
mongoose: mongoose,
useAccessToken: true,
redirectLogin: '/account',
redirectSignup: '/account',
mongoUser: db.User,
connectors: {
github: {
clientId: "xxxxxxx",
clientSecret: "xxxxxx",
redirectUri: "https://my.app/auth/github/callback"
},
google: {
clientId: 'xxxxxxx.apps.googleusercontent.com',
clientSecret: 'xxxxxxxxx',
redirectUri: 'https://my.app/auth/google/callback'
}
},
pages: {
logoUrl: '/logo.png',
customHeadHtml: `
<link href="https://fonts.googleapis.com/css?family=Muli:400,800,900&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/css/login.css">
`,
}
})
const logged = require('express-account-block/logged')(db.User)
app.get('/dashboard', logged, (req, res, next) => {
const user = res.locals.user
...
})
Account page
Will require Bootstrap and jQuery.
In your express route:
app.get("/account", (req, res, next) => {
res.render("my_account_view", {
accountTemplate: require.resolve("express-account-block/account.ejs")
})
})
Make sure to have Bootstrap.js and jQuery included.
Then, in your ejs:
<body>
<%- include(accountTemplate) %>
</body>
<script src="/account/account.js" defer></script>
Oauth
Currently the module supports Github and Google oauth.
The redirect URIs are predefined paths:
- /auth/github/callback for Github
- /auth/google/callback for Google