Skip to content

Commit

Permalink
JWT Fixes (#14)
Browse files Browse the repository at this point in the history
* Fix not building example

* Allow setting expires in via environment
  • Loading branch information
joshgachnang authored Dec 6, 2021
1 parent d238c45 commit bfea361
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/example.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import chai from "chai";
import express, {Express} from "express";
import express from "express";
import mongoose, {model, Schema} from "mongoose";
import passportLocalMongoose from "passport-local-mongoose";
import supertest from "supertest";
import {tokenPlugin} from ".";
import {
AdminOwnerTransformer,
baseUserPlugin,
createdDeletedPlugin,
gooseRestRouter,
Permissions,
setupAuth,
baseUserPlugin,
createdDeletedPlugin,
} from "./mongooseRestFramework";

const assert = chai.assert;
Expand Down
9 changes: 6 additions & 3 deletions src/mongooseRestFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,19 @@ export function checkPermissions<T>(
return anyTrue;
}

export function tokenPlugin(schema: Schema) {
export function tokenPlugin(schema: Schema, options: {expiresIn?: number} = {}) {
schema.add({token: {type: String, index: true}});
schema.pre("save", function(next) {
// Add created when creating the object
if (!this.token) {
const options: any = {
const tokenOptions: any = {
expiresIn: "10h",
};
if ((process.env as any).TOKEN_EXPIRES_IN) {
tokenOptions.issuer = (process.env as any).TOKEN_EXPIRES_IN;
}
if ((process.env as any).TOKEN_ISSUER) {
options.issuer = (process.env as any).TOKEN_ISSUER;
tokenOptions.issuer = (process.env as any).TOKEN_ISSUER;
}
this.token = jwt.sign({id: this._id.toString()}, (process.env as any).TOKEN_SECRET, options);
}
Expand Down

0 comments on commit bfea361

Please sign in to comment.