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

Commit

Permalink
feat: package generateQRCode function
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Mar 27, 2020
1 parent 575ecf1 commit cc50b7e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
6 changes: 5 additions & 1 deletion schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ input LoginInput {

type Mutation {
register(input: RegisterInput!): AuthModel!
totp: AuthModel!
totp: TOTPModel!
createAnnouncement(input: CreateAnnouncementInput!): AnnouncementModel!
updateAnnouncementById(input: UpdateAnnouncementInput!): AnnouncementModel!
deleteAnnouncementById(id: ID!): AnnouncementModel!
Expand Down Expand Up @@ -278,6 +278,10 @@ type SMSModel {
updatedAt: DateTime!
}

type TOTPModel {
qrcode: String!
}

input UpdateAgendaInput {
id: String!
title: String
Expand Down
3 changes: 2 additions & 1 deletion src/auth/auth.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Args, Query, Resolver, Mutation } from '@nestjs/graphql'
import { AuthService } from './auth.service'
import { AuthModel } from './models/auth.model'
import { TOTPModel } from './models/totp.model'
import { LoginInput } from './dtos/login.input'
import { RegisterInput } from './dtos/register.input'

Expand All @@ -20,7 +21,7 @@ export class AuthResolver {
return this.authService.register(input)
}

@Mutation(() => AuthModel)
@Mutation(() => TOTPModel)
public async totp() {
return this.authService.totp()
}
Expand Down
4 changes: 2 additions & 2 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'
import { ForbiddenError, AuthenticationError } from 'apollo-server-express'
import { JwtService } from '@nestjs/jwt'
import speakeasy from 'speakeasy'
import QRCode from 'qrcode'
import { generateQRCode } from '../shared/utils'
import { UsersService } from '../users/users.service'
import { Roles, User } from '../users/interfaces/user.interface'
import { LoginInput } from './dtos/login.input'
Expand Down Expand Up @@ -60,6 +60,6 @@ export class AuthService {
public async totp() {
const { base32, otpauth_url } = speakeasy.generateSecret()

QRCode.toDataURL(otpauth_url, (err, data_url) => ({ qrcode: data_url }))
return generateQRCode(otpauth_url)
}
}
7 changes: 7 additions & 0 deletions src/auth/models/totp.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Field, ObjectType } from '@nestjs/graphql'

@ObjectType()
export class TOTPModel {
@Field()
public readonly qrcode: string
}
12 changes: 12 additions & 0 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import QRCode from 'qrcode'
import { ApolloError } from 'apollo-server-express'

export const generateSMSVerificationCode = () =>
(Math.floor(Math.random() * 10000) + 10000).toString().slice(1)

export const jsonStringify = <T>(obj: T) => JSON.stringify(obj).replace(/"([^(")"]+)":/g, '$1:')

export const generateQRCode = async (url: string) => {
try {
const qrcode = await QRCode.toDataURL(url)
return { qrcode }
} catch (err) {
throw new ApolloError('Generate QR code failed!')
}
}

0 comments on commit cc50b7e

Please sign in to comment.