1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-03 21:10:05 +00:00

chore(merge): Merge branch 'master' into authentication-with-jwt

This commit is contained in:
Saad Jutt
2021-11-09 18:24:35 +05:00
83 changed files with 43080 additions and 10960 deletions

View File

@@ -0,0 +1,44 @@
import { Security, Route, Tags, Example, Post, Body } from 'tsoa'
import Client, { ClientPayload } from '../model/Client'
@Security('bearerAuth')
@Route('SASjsApi/client')
@Tags('Client')
export default class ClientController {
/**
* Create client with the following attributes: ClientId, ClientSecret. Admin only task.
*
*/
@Example<ClientPayload>({
clientId: 'someFormattedClientID1234',
clientSecret: 'someRandomCryptoString'
})
@Post('/')
public async createClient(
@Body() body: ClientPayload
): Promise<ClientPayload> {
return createClient(body)
}
}
const createClient = async (data: any): Promise<ClientPayload> => {
const { clientId, clientSecret } = data
// Checking if client is already in the database
const clientExist = await Client.findOne({ clientId })
if (clientExist) throw new Error('Client ID already exists.')
// Create a new client
const client = new Client({
clientId,
clientSecret
})
const savedClient = await client.save()
return {
clientId: savedClient.clientId,
clientSecret: savedClient.clientSecret
}
}