Skip to content

Commit

Permalink
fixed default config
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-chripko committed Apr 12, 2023
1 parent c5f2cb6 commit 66f501c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
49 changes: 49 additions & 0 deletions config/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ExtractJwt } from 'passport-jwt'
import { IPassportJwtWrapperConfig, IPassportConfig } from '../src/types/config'

export default {
passportJwtWrapper: <Partial<IPassportJwtWrapperConfig>>{
checkAccessToken: false,
i18next: {
preload: ['en', 'sk'],
fallbackLng: 'en',
ns: ['error', 'translation'],
defaultNS: 'translation',
detection: {
order: ['header']
},
backend: {
loadPath: 'locales/{{lng}}/{{ns}}.json',
jsonIndent: 2
},
nsSeparator: ':',
keySeparator: false
},
passport: <IPassportConfig>{
local: {
usernameField: 'email',
passwordField: 'password',
session: false,
passReqToCallback: true
},
jwt: {
secretOrKey: process.env.JWT_SECRET,
api: {
exp: '15m',
jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeaderAsBearerToken(), ExtractJwt.fromUrlQueryParameter('t')]),
refresh: {
exp: '4h'
}
},
passwordReset: {
exp: '4h',
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
},
invitation: {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
exp: '30d'
}
}
}
}
}
61 changes: 6 additions & 55 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ import {
import { IPassportConfig, IPassportJwtWrapperConfig } from './types/config'
import { State } from './State'
import { JWT_AUDIENCE, PASSPORT_NAME } from './utils/enums'
import { createHash } from './utils/jwt'
import { createHash, createJwt } from './utils/jwt'
/* eslint-disable import/first */
import defaultConfigs from '../config/default'

process.env.SUPPRESS_NO_CONFIG_WARNING = 'y'

config.util.setModuleDefaults('passportJwtWrapper', defaultConfigs.passportJwtWrapper)

/**
* Initialization method, have to be run before using this authentication library
* repositories are: {
Expand All @@ -48,60 +52,6 @@ function initAuth<TokenIDType extends ID, UserIDType extends ID>(
passwordResetTokenRepository?: IPasswordResetTokenRepository<UserIDType>
}
) {
const i18nextConfig = <InitOptions>{
preload: ['en', 'sk'],
fallbackLng: 'en',
ns: ['error', 'translation'],
defaultNS: 'translation',
detection: {
order: ['header']
},
backend: {
loadPath: 'locales/{{lng}}/{{ns}}.json',
jsonIndent: 2
},
nsSeparator: ':',
keySeparator: false,
returnNull: false
}

const passportConfig = <IPassportConfig>{
local: {
usernameField: 'email',
passwordField: 'password',
session: false,
passReqToCallback: true
},
jwt: {
secretOrKey: process.env.JWT_SECRET,
api: {
exp: '15m',
jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeaderAsBearerToken(), ExtractJwt.fromUrlQueryParameter('t')]),
refresh: {
exp: '4h'
}
},
passwordReset: {
exp: '4h',
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
},
invitation: {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
exp: '30d'
}
}
}

const defaultConfigs: LibConfig = {
checkAccessToken: false,
i18next: i18nextConfig,
passport: passportConfig
}

if (!config.has('passportJwtWrapper')) {
config.util.setModuleDefaults('passportJwtWrapper', defaultConfigs)
}

const instance = State.initialize(
passport,
repositories.userRepository,
Expand Down Expand Up @@ -134,6 +84,7 @@ export {
JWT_AUDIENCE,
// helper functions
createHash,
createJwt,
// types
IPassportJwtWrapperConfig,
IPassportConfig,
Expand Down

0 comments on commit 66f501c

Please sign in to comment.