login with api
This commit is contained in:
parent
d8a9909eb9
commit
ff5b189b2f
16 changed files with 1241 additions and 66 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { defineStore } from 'pinia';
|
||||
import { authService } from '~/services/auth.service';
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
|
|
@ -22,32 +23,26 @@ export const useAuthStore = defineStore('auth', {
|
|||
|
||||
actions: {
|
||||
async login(email: string, password: string) {
|
||||
// TODO: Replace with real API call
|
||||
// const { $api } = useNuxtApp();
|
||||
// const response = await $api('/auth/login', {
|
||||
// method: 'POST',
|
||||
// body: { email, password }
|
||||
// });
|
||||
try {
|
||||
// Call real API
|
||||
const response = await authService.login(email, password);
|
||||
|
||||
// Mock login for development
|
||||
const mockUser: User = {
|
||||
id: '1',
|
||||
email: email,
|
||||
fullName: 'อาจารย์ทดสอบ',
|
||||
role: 'INSTRUCTOR'
|
||||
};
|
||||
this.token = response.token;
|
||||
this.user = response.user as User;
|
||||
this.isAuthenticated = true;
|
||||
|
||||
this.token = 'mock-jwt-token';
|
||||
this.user = mockUser;
|
||||
this.isAuthenticated = true;
|
||||
// Save to localStorage (including refreshToken)
|
||||
if (process.client) {
|
||||
localStorage.setItem('token', this.token);
|
||||
localStorage.setItem('refreshToken', response.refreshToken);
|
||||
localStorage.setItem('user', JSON.stringify(this.user));
|
||||
}
|
||||
|
||||
// Save to localStorage
|
||||
if (process.client) {
|
||||
localStorage.setItem('token', this.token);
|
||||
localStorage.setItem('user', JSON.stringify(this.user));
|
||||
return { token: this.token, user: this.user };
|
||||
} catch (error: any) {
|
||||
// Re-throw error to be handled by login page
|
||||
throw error;
|
||||
}
|
||||
|
||||
return { token: this.token, user: this.user };
|
||||
},
|
||||
|
||||
logout() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue