auth.ts 673 B

1234567891011121314151617181920212223242526272829303132333435
  1. import http from '@/lib/http'
  2. import { useUserStore } from '@/pinia'
  3. const { login, logout } = useUserStore()
  4. export interface AuthResponse {
  5. token: string
  6. }
  7. const auth = {
  8. async login(name: string, password: string) {
  9. return http.post('/login', {
  10. name,
  11. password,
  12. }).then((r: AuthResponse) => {
  13. login(r.token)
  14. })
  15. },
  16. async casdoor_login(code?: string, state?: string) {
  17. await http.post('/casdoor_callback', {
  18. code,
  19. state,
  20. })
  21. .then((r: AuthResponse) => {
  22. login(r.token)
  23. })
  24. },
  25. logout() {
  26. return http.delete('/logout').then(async () => {
  27. logout()
  28. })
  29. },
  30. }
  31. export default auth