Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional authFields for JWT payload. #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/guards.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router, Handler } from 'express'
import * as jwt from 'jsonwebtoken'
import * as jsonServer from 'json-server'
import { stringify } from 'querystring'
import { stringify, ParsedUrlQueryInput } from 'querystring'
import { JWT_SECRET_KEY } from './constants'
import { bodyParsingHandler, errorHandler, goNext } from './shared-middlewares'

Expand Down Expand Up @@ -66,7 +66,7 @@ const privateOnly: Handler = (req, res, next) => {
}

// TODO: handle query params instead of removing them
const path = req.url.replace(`?${stringify(req.query)}`, '')
const path = req.url.replace(`?${stringify(req.query as ParsedUrlQueryInput)}`, '')
const [, mod, resource, id] = path.split('/')

// Creation and replacement
Expand Down
16 changes: 12 additions & 4 deletions src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ const validate: ValidateHandler = ({ required }) => (req, res, next) => {
* Register / Create a user
*/
const create: Handler = (req, res, next) => {
const { email, password, ...rest } = req.body as User
const { email, password, authFields, ...rest } = req.body as User
const { db } = req.app

const fields = {};
if (authFields)
authFields.forEach(field => fields[field] = req.body[field]);

if (db == null) {
// json-server CLI expose the router db to the app
// (https://github.com/typicode/json-server/blob/master/src/cli/run.js#L74),
Expand All @@ -71,7 +75,7 @@ const create: Handler = (req, res, next) => {
try {
return db
.get('users')
.insert({ email, password: hash, ...rest })
.insert({ email, password: hash, authFields, ...rest })
.write()
} catch (error) {
throw Error('You must add a "users" collection to your db')
Expand All @@ -80,7 +84,7 @@ const create: Handler = (req, res, next) => {
.then((user: User) => {
return new Promise<string>((resolve, reject) => {
jwt.sign(
{ email },
{ email, ...fields },
JWT_SECRET_KEY,
{ expiresIn: JWT_EXPIRES_IN, subject: String(user.id) },
(error, idToken) => {
Expand Down Expand Up @@ -115,14 +119,18 @@ const login: Handler = (req, res, next) => {
return
}

const fields = {};
if (user.authFields)
user.authFields.forEach(field => fields[field] = user[field]);

bcrypt
.compare(password, user.password)
.then((same) => {
if (!same) throw 400

return new Promise<string>((resolve, reject) => {
jwt.sign(
{ email },
{ email, ...fields },
JWT_SECRET_KEY,
{ expiresIn: JWT_EXPIRES_IN, subject: String(user.id) },
(error, idToken) => {
Expand Down