Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
feat: update decodeJWT
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Apr 20, 2020
1 parent 864ec81 commit cff0ea2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { RegisterInput } from './dtos/register.input'
import { ValidateTOTPInput } from './dtos/validate-totp.input'
import { ChangePasswordInput } from './dtos/change-password.input'
import { TOTP_ENCODE, IP_STACK_URL } from '../shared/constants'
import { generateQRCode, generateRecoveryCodes, decodeJwt, encryptPassword } from '../shared/utils'
import { generateQRCode, generateRecoveryCodes, decodeJWT, encryptPassword } from '../shared/utils'

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -73,7 +73,7 @@ export class AuthService {
}

public async createTOTP(token: string) {
const { email } = decodeJwt(token)
const { email } = decodeJWT(token)
const { base32, otpauth_url } = speakeasy.generateSecret({
name: email,
})
Expand All @@ -83,7 +83,7 @@ export class AuthService {
}

public async validateTOTP(input: ValidateTOTPInput, token: string) {
const { sub: userId } = decodeJwt(token)
const { sub: userId } = decodeJWT(token)
const { key, code } = input

const verified = speakeasy.totp.verify({
Expand All @@ -101,7 +101,7 @@ export class AuthService {
}

public async createRecoveryCodes(token: string) {
const { sub: userId } = decodeJwt(token)
const { sub: userId } = decodeJWT(token)

const codes = generateRecoveryCodes()
const res = await this.usersService.updateUser({ id: userId, recoveryCodes: codes })
Expand All @@ -110,7 +110,7 @@ export class AuthService {
}

public async validateRecoveryCode(input: ValidateTOTPInput, token: string) {
const { sub: userId } = decodeJwt(token)
const { sub: userId } = decodeJWT(token)

const { code } = input
const { recoveryCodes } = await this.usersService.findOneById(userId)
Expand All @@ -131,7 +131,7 @@ export class AuthService {

public async changePassword(input: ChangePasswordInput, token: string) {
const { oldPassword, newPassword } = input
const { sub: userId } = decodeJwt(token)
const { sub: userId } = decodeJWT(token)
const user = await this.usersService.findOneById(userId)

if (user && user.isValidPassword(oldPassword, user.password)) {
Expand All @@ -149,7 +149,7 @@ export class AuthService {
const IP_STACK_ACCESS_KEY = this.configService.getIpStackAccessKey()
const token = req.headers.authorization
const userAgent = req.headers['user-agent']
const { sub: userId } = decodeJwt(token)
const { sub: userId } = decodeJWT(token)
const ip = requestIP.getClientIp(req)

const network = {
Expand Down
1 change: 0 additions & 1 deletion src/global-setting/global-setting.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class GlobalSettingResolver {
}

@Query(() => GlobalSettingModel)
@UseGuards(GqlAuthGuard)
public async getGlobalSetting() {
return this.globalSettingService.findOne()
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export const generateRecoveryCodes = () => {
return codes
}

export const decodeJwt = (token: string) => jwt.decode(token.slice(7)) as Payload
export const decodeJWT = (token: string) => jwt.decode(token.slice(7)) as Payload

export const encryptPassword = (password: string) => bcrypt.hashSync(password, 10)
4 changes: 2 additions & 2 deletions src/sms/sms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { UsersService } from '../users/users.service'
import { SMS, AliSMSParams } from './interfaces/sms.interface'
import { ValidateSMSInput } from './dtos/validateSMS.input'
import { ALI_SMS_END_POINT, ALI_SMS_API_VERSION, ALI_SMS_REGION } from '../shared/constants'
import { decodeJwt } from '../shared/utils'
import { decodeJWT } from '../shared/utils'

@Injectable()
export class SMSService {
Expand Down Expand Up @@ -78,7 +78,7 @@ export class SMSService {
}

public async validateSMSVerificationCode(input: ValidateSMSInput, token: string) {
const { sub: userId } = decodeJwt(token)
const { sub: userId } = decodeJWT(token)

const { phoneNumber, smsCode } = input

Expand Down
4 changes: 2 additions & 2 deletions src/users/users.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UserModel } from './models/user.model'
import { UpdateUserInput } from './dtos/update-user.input'
import { GqlAuthGuard } from '../shared/guard/gqlAuth.guard'
import { ReqDecorator } from '../shared/decorators/req.decorator'
import { decodeJwt } from '../shared/utils'
import { decodeJWT } from '../shared/utils'

@Resolver(() => UserModel)
export class UserResolver {
Expand All @@ -17,7 +17,7 @@ export class UserResolver {
@Mutation(() => UserModel)
@UseGuards(GqlAuthGuard)
public async updateUser(@Args('input') input: UpdateUserInput, @ReqDecorator() req: Request) {
const { sub: userId } = decodeJwt(req.headers.authorization)
const { sub: userId } = decodeJWT(req.headers.authorization)
return this.usersService.updateUser({ ...input, id: userId })
}

Expand Down
8 changes: 4 additions & 4 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ForbiddenError } from 'apollo-server-express'
import { User } from './interfaces/user.interface'
import { UpdateUserInput } from './dtos/update-user.input'
import { RegisterInput } from '../auth/dtos/register.input'
import { decodeJwt } from '../shared/utils'
import { decodeJWT } from '../shared/utils'

@Injectable()
export class UsersService {
Expand Down Expand Up @@ -43,7 +43,7 @@ export class UsersService {
}

public async updateUserName(username: string, req: Request): Promise<User> {
const { sub: id } = decodeJwt(req.headers.authorization)
const { sub: id } = decodeJWT(req.headers.authorization)
const user = await this.findOneByUserName(username)

if (!user) {
Expand All @@ -54,7 +54,7 @@ export class UsersService {
}

public async updateEmail(email: string, req: Request): Promise<User> {
const { sub: id } = decodeJwt(req.headers.authorization)
const { sub: id } = decodeJWT(req.headers.authorization)
const user = await this.findOneByEmail(email)

if (!user) {
Expand All @@ -64,7 +64,7 @@ export class UsersService {
}

public async deleteOneById(req: Request): Promise<User> {
const { sub: id } = decodeJwt(req.headers.authorization)
const { sub: id } = decodeJWT(req.headers.authorization)
return this.UserModel.findByIdAndDelete(id)
}
}

0 comments on commit cff0ea2

Please sign in to comment.