Client - authorize oauth client

This commit is contained in:
Sam
2022-06-07 17:45:08 +02:00
parent d8d88cda3a
commit 4be4f46c26
12 changed files with 196 additions and 17 deletions

View File

@ -5,10 +5,60 @@ import router from '@/router'
import { OAUTH2_STORE, ROOT_STORE } from '@/store/constants'
import { IOAuth2Actions, IOAuth2State } from '@/store/modules/oauth2/types'
import { IRootState } from '@/store/modules/root/types'
import { IOauth2ClientsPayload, IOAuth2ClientPayload } from '@/types/oauth'
import {
IOauth2ClientsPayload,
IOAuth2ClientPayload,
IOAuth2ClientAuthorizePayload,
} from '@/types/oauth'
import { handleError } from '@/utils'
const get_client = (
context: ActionContext<IOAuth2State, IRootState>,
url: string
) => {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.get(url)
.then((res) => {
if (res.data.status === 'success') {
context.commit(OAUTH2_STORE.MUTATIONS.SET_CLIENT, res.data.data.client)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
}
export const actions: ActionTree<IOAuth2State, IRootState> & IOAuth2Actions = {
[OAUTH2_STORE.ACTIONS.AUTHORIZE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOAuth2ClientAuthorizePayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
const form = new FormData()
form.set('client_id', payload.client_id)
form.set('response_type', payload.response_type)
form.set('scope', payload.scope)
form.set('confirm', 'true')
if (payload.state) {
form.set('state', payload.state)
}
authApi
.post('oauth/authorize', form, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then((res) => {
if (res.status == 200 && res.data.redirect_url) {
window.location.href = res.data.redirect_url
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
},
[OAUTH2_STORE.ACTIONS.CREATE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOAuth2ClientPayload
@ -47,24 +97,17 @@ export const actions: ActionTree<IOAuth2State, IRootState> & IOAuth2Actions = {
})
.catch((error) => handleError(context, error))
},
[OAUTH2_STORE.ACTIONS.GET_CLIENT_BY_CLIENT_ID](
context: ActionContext<IOAuth2State, IRootState>,
client_id: string
): void {
get_client(context, `oauth/apps/${client_id}`)
},
[OAUTH2_STORE.ACTIONS.GET_CLIENT_BY_ID](
context: ActionContext<IOAuth2State, IRootState>,
id: number
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.get(`oauth/apps/${id}/by_id`)
.then((res) => {
if (res.data.status === 'success') {
context.commit(
OAUTH2_STORE.MUTATIONS.SET_CLIENT,
res.data.data.client
)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
get_client(context, `oauth/apps/${id}/by_id`)
},
[OAUTH2_STORE.ACTIONS.GET_CLIENTS](
context: ActionContext<IOAuth2State, IRootState>,

View File

@ -1,7 +1,9 @@
export enum OAuth2Actions {
AUTHORIZE_CLIENT = 'AUTHORIZE_CLIENT',
CREATE_CLIENT = 'CREATE_CLIENT',
DELETE_CLIENT = 'DELETE_CLIENT',
GET_CLIENTS = 'GET_CLIENTS',
GET_CLIENT_BY_CLIENT_ID = 'GET_CLIENT_BY_CLIENT_ID',
GET_CLIENT_BY_ID = 'GET_CLIENT_BY_ID',
}

View File

@ -10,6 +10,7 @@ import { IRootState } from '@/store/modules/root/types'
import { IPagination } from '@/types/api'
import {
IOAuth2Client,
IOAuth2ClientAuthorizePayload,
IOAuth2ClientPayload,
IOauth2ClientsPayload,
} from '@/types/oauth'
@ -21,6 +22,10 @@ export interface IOAuth2State {
}
export interface IOAuth2Actions {
[OAUTH2_STORE.ACTIONS.AUTHORIZE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOAuth2ClientAuthorizePayload
): void
[OAUTH2_STORE.ACTIONS.CREATE_CLIENT](
context: ActionContext<IOAuth2State, IRootState>,
payload: IOAuth2ClientPayload
@ -29,6 +34,10 @@ export interface IOAuth2Actions {
context: ActionContext<IOAuth2State, IRootState>,
id: number
): void
[OAUTH2_STORE.ACTIONS.GET_CLIENT_BY_CLIENT_ID](
context: ActionContext<IOAuth2State, IRootState>,
client_id: string
): void
[OAUTH2_STORE.ACTIONS.GET_CLIENT_BY_ID](
context: ActionContext<IOAuth2State, IRootState>,
id: number