Skip to content

Commit

Permalink
gh-238. Add conditional validation for auth route
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhbankov-rpp committed Mar 14, 2021
1 parent 2843250 commit 431b95f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 35 deletions.
4 changes: 0 additions & 4 deletions apps/web-portal/lib/rest-api-v1/auth.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import {AuthLogin} from '../use-cases/auth'
import {AuthLoginToken} from '../use-cases/auth'

export default ({makeRunner}) => {
return {
login: makeRunner(AuthLogin, {
toParams: (req) => ({...req.body}),
}),
loginWithToken: makeRunner(AuthLoginToken, {
toParams: (req) => ({...req.body}),
}),
}
}
1 change: 0 additions & 1 deletion apps/web-portal/lib/rest-api-v1/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function route(deps) {
router.get('/specs', specController.list)
router.get('/specs/:id', specController.read)
router.post('/auth/login', authController.login)
router.post('/auth/login/token', authController.loginWithToken)
router.post('/log', logController.save)

return router
Expand Down
22 changes: 20 additions & 2 deletions apps/web-portal/lib/use-cases/auth/AuthLogin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@ import {apiToken} from '../utils'

export class AuthLogin extends UseCase {
static rules = {
email: ['required', 'email'],
password: 'required',
credentials: [{
or: [{
nested_object: {
password: ['required'],
email: ['required', 'email']
}
}, {
nested_object: {
token: ['required']
}
}]
}, 'required']
}


async run(params) {
// eslint-disable-next-line no-unused-vars
const {credentials: { token, password, email }} = params

if (token) {
apiToken.verify(token)
}

return {
token: apiToken.sign(params),
email: '[email protected]',
Expand Down
21 changes: 0 additions & 21 deletions apps/web-portal/lib/use-cases/auth/AuthLoginToken.mjs

This file was deleted.

1 change: 0 additions & 1 deletion apps/web-portal/lib/use-cases/auth/index.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './AuthLogin'
export * from './AuthLoginToken'
7 changes: 1 addition & 6 deletions packages/util-web-api-client/lib/axios/AxiosAuthApi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ export class AxiosAuthApi extends AuthApi {
}

async login(params) {
let response
if (params.token) {
response = await this.#http.post('/api/v1/auth/login/token', params)
} else {
response = await this.#http.post('/api/v1/auth/login', params)
}
const response = await this.#http.post('/api/v1/auth/login', { credentials: { ...params } })
return response ? response.data : {status: 1}
}
}

0 comments on commit 431b95f

Please sign in to comment.