Skip to content

Commit 49174ad

Browse files
Merge pull request #22 from QuickBlox/dev/v2.0.0-alpha.3
Release v2.0.0-alpha.3
2 parents 952359b + a33be15 commit 49174ad

File tree

43 files changed

+739
-78
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+739
-78
lines changed

bin/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const fields = [
2121
'SERVER_APP_URL',
2222
'DEFAULT_LANGUAGE',
2323
'FILE_SIZE_LIMIT',
24-
'FILE_EXTENSIONS_WHITELIST'
24+
'FILE_EXTENSIONS_WHITELIST',
25+
'ENABLE_GUEST_CLIENT',
2526
]
2627

2728
const parseValue = (value) => {

bin/initConfig.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function getBaseConfiguration() {
3939
const ENABLE_REDUX_LOGGER = readline.keyInYN(`Enable Redux logger?\n`) || false
4040
const CLIENT_APP_URL = readline.question(`Enter Client app URL (optional):\n`, { defaultInput: DEFAULT_CLIENT_APP_URL, limit: /^https?:\/\/[^\s\/$.?#].[^\s]*$/ })
4141
const SERVER_APP_URL = readline.question(`Enter Server app URL (optional):\n`, { defaultInput: DEFAULT_SERVER_APP_URL, limit: /^https?:\/\/[^\s\/$.?#].[^\s]*$/ })
42+
const ENABLE_GUEST_CLIENT = readline.keyInYN(`Enable Guest Client?\n`) || false
4243
const DEFAULT_LANGUAGE = readline.question(`Enter default language (optional) [en/ua]:\n`, { defaultInput: 'en', limit: ['en', 'ua'] })
4344
const FILE_SIZE_LIMIT = readline.questionInt(`Enter a file size limit in bytes (optional):\n`, { defaultInput: DEFAULT_FILE_SIZE_LIMIT })
4445
const FILE_EXTENSIONS_WHITELIST = readline.question(`Enter a space-separated list of available file extensions (optional):\n`, { defaultInput: DEFAULT_FILE_EXTENSIONS_WHITELIST })
@@ -65,6 +66,7 @@ function getBaseConfiguration() {
6566
DEFAULT_LANGUAGE,
6667
FILE_SIZE_LIMIT,
6768
FILE_EXTENSIONS_WHITELIST,
69+
ENABLE_GUEST_CLIENT,
6870
}
6971
}
7072

packages/api/src/plugins/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const schema = Type.Object({
2020
AI_QUICK_ANSWER: Type.Boolean({ default: false }),
2121
AI_SUGGEST_PROVIDER: Type.Boolean({ default: false }),
2222
AI_RECORD_ANALYTICS: Type.Boolean({ default: false }),
23+
ENABLE_GUEST_CLIENT: Type.Boolean({ default: false }),
2324
})
2425

2526
const options = {

packages/api/src/routes/appointments/getMyList.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
2-
import { Type } from '@sinclair/typebox'
3-
import { QBAppointment } from 'quickblox'
2+
import { Static, Type } from '@sinclair/typebox'
3+
import { QBAppointment, QBSession } from 'quickblox'
44
import omit from 'lodash/omit'
55

66
import {
@@ -61,10 +61,32 @@ const getMyAppointmentListSchema = {
6161
}
6262

6363
const getMyAppointmentList: FastifyPluginAsyncTypebox = async (fastify) => {
64+
const handleValidate = async (
65+
session: QBSession,
66+
query: Static<typeof getMyAppointmentListSchema.querystring>,
67+
) => {
68+
const { provider_id, client_id } = query
69+
const user = await findUserById(session.user_id)
70+
const isProvider = userHasTag(user!, 'provider')
71+
72+
if (isProvider && provider_id) {
73+
return fastify.httpErrors.forbidden('body/provider_id Forbidden property')
74+
}
75+
76+
if (!isProvider && client_id) {
77+
return fastify.httpErrors.forbidden('body/client_id Forbidden property')
78+
}
79+
80+
return undefined
81+
}
82+
6483
fastify.get(
6584
'/my',
6685
{
6786
schema: getMyAppointmentListSchema,
87+
preHandler: (request, reply, done) => {
88+
handleValidate(request.session!, request.query).then(done).catch(done)
89+
},
6890
onRequest: fastify.verify(fastify.SessionToken),
6991
},
7092
async (request) => {

packages/api/src/routes/auth/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const login: FastifyPluginAsyncTypebox = async (fastify) => {
2828
fastify.post('/login', { schema: loginSchema }, async (request, reply) => {
2929
const { role, email, password } = request.body
3030
const session = await qbCreateSession()
31-
const user = await qbLogin(email, password)
31+
const user = await qbLogin({ email, password })
3232
const isProvider = userHasTag(user, 'provider')
3333

3434
if (role === 'provider' ? isProvider : !isProvider) {

packages/api/src/routes/users/client/signup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const signup: FastifyPluginAsyncTypebox = async (fastify) => {
5757
...userData,
5858
custom_data: stringifyUserCustomData(customData),
5959
})
60-
let user = await qbLogin(email, password)
60+
let user = await qbLogin({ email, password })
6161

6262
if (avatar) {
6363
const file = await qbUploadFile(avatar)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
2+
import { Type } from '@sinclair/typebox'
3+
import { QBCreateUserWithLogin } from 'quickblox'
4+
import { randomBytes } from 'crypto'
5+
6+
import { QBSession, QBUser, QCClient } from '@/models'
7+
import { qbCreateSession, qbLogin } from '@/services/quickblox/auth'
8+
import { qbCreateUser } from '@/services/quickblox/users'
9+
10+
export const createGuestClientSchema = {
11+
tags: ['Users', 'Client'],
12+
summary: 'Create Guest client',
13+
body: Type.Pick(QCClient, ['full_name']),
14+
response: {
15+
200: Type.Object({
16+
session: Type.Ref(QBSession),
17+
user: Type.Ref(QBUser),
18+
}),
19+
},
20+
security: [{ apiKey: [] }, { providerSession: [] }] as Security,
21+
}
22+
23+
const createGuestClient: FastifyPluginAsyncTypebox = async (fastify) => {
24+
fastify.post('', { schema: createGuestClientSchema }, async (request) => {
25+
const { full_name } = request.body
26+
const login = Date.now().toString()
27+
const password = randomBytes(8).toString('hex')
28+
const session = await qbCreateSession()
29+
30+
await qbCreateUser<QBCreateUserWithLogin>({
31+
login,
32+
password,
33+
full_name,
34+
tag_list: 'guest',
35+
})
36+
const user = await qbLogin({ login, password })
37+
38+
return { session, user }
39+
})
40+
}
41+
42+
export const autoload = JSON.parse<boolean>(process.env.ENABLE_GUEST_CLIENT!)
43+
44+
export default createGuestClient

packages/api/src/routes/users/provider/signup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const signup: FastifyPluginAsyncTypebox = async (fastify) => {
7070
}),
7171
tag_list: ['provider'],
7272
})
73-
let user = await qbLogin(email, password)
73+
let user = await qbLogin({ email, password })
7474

7575
if (avatar) {
7676
const file = await qbUploadFile(avatar)

packages/api/src/services/quickblox/auth.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,19 @@ export const qbGetSession = () => {
6969
})
7070
}
7171

72-
export const qbLogin = (email: string, password: string) =>
72+
type LoginCredentials =
73+
| {
74+
login: string
75+
password: string
76+
}
77+
| {
78+
email: string
79+
password: string
80+
}
81+
82+
export const qbLogin = (credentials: LoginCredentials) =>
7383
new Promise<QBUser>((resolve, reject) => {
74-
QB.login({ email, password }, (error, result) => {
84+
QB.login(credentials, (error, result) => {
7585
if (error) {
7686
reject(error)
7787
} else {

packages/api/src/types/quickblox.d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ declare module 'quickblox' {
459459
login: string
460460
password: string
461461
blob_id?: number
462-
custom_data: string | null
462+
custom_data?: string | null
463463
email?: string
464464
external_user_id?: string | number
465465
facebook_id?: string
@@ -538,6 +538,20 @@ declare module 'quickblox' {
538538
): void
539539
}
540540

541+
type QBLoginParams =
542+
| {
543+
login: string
544+
password: string
545+
}
546+
| {
547+
email: string
548+
password: string
549+
}
550+
| {
551+
provider: 'firebase_phone'
552+
firebase_phone: { access_token: string; project_id: string }
553+
}
554+
541555
interface Quickblox {
542556
buildNumber: string
543557
chat: QBChatModule

0 commit comments

Comments
 (0)