auth.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import type { AuthenticationResponseJSON } from '@simplewebauthn/browser'
  2. import { http } from '@uozi-admin/request'
  3. import { useUserStore } from '@/pinia'
  4. const { login, logout } = useUserStore()
  5. export interface AuthResponse {
  6. message: string
  7. token: string
  8. short_token: string
  9. code: number
  10. error: string
  11. secure_session_id: string
  12. }
  13. const auth = {
  14. async login(name: string, password: string, otp: string, recoveryCode: string): Promise<AuthResponse> {
  15. return http.post('/login', {
  16. name,
  17. password,
  18. otp,
  19. recovery_code: recoveryCode,
  20. }, { crypto: true })
  21. },
  22. async casdoor_login(code?: string, state?: string) {
  23. await http.post('/casdoor_callback', {
  24. code,
  25. state,
  26. })
  27. .then((r: AuthResponse) => {
  28. login(r.token, r.short_token)
  29. })
  30. },
  31. async logout() {
  32. return http.delete('/logout').then(async () => {
  33. logout()
  34. })
  35. },
  36. async get_casdoor_uri(): Promise<{ uri: string }> {
  37. return http.get('/casdoor_uri')
  38. },
  39. begin_passkey_login() {
  40. return http.get('/begin_passkey_login')
  41. },
  42. finish_passkey_login(data: { session_id: string, options: AuthenticationResponseJSON }) {
  43. return http.post('/finish_passkey_login', data.options, {
  44. headers: {
  45. 'X-Passkey-Session-Id': data.session_id,
  46. },
  47. })
  48. },
  49. }
  50. export default auth