38 lines
752 B
JavaScript
38 lines
752 B
JavaScript
|
|
export class Auth {
|
||
|
|
constructor() {
|
||
|
|
this.currentUser = null;
|
||
|
|
this.isAuthenticated = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
login(email, password) {
|
||
|
|
if (email && password) {
|
||
|
|
this.currentUser = { email };
|
||
|
|
this.isAuthenticated = true;
|
||
|
|
localStorage.setItem('user', JSON.stringify(this.currentUser));
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
logout() {
|
||
|
|
this.currentUser = null;
|
||
|
|
this.isAuthenticated = false;
|
||
|
|
localStorage.removeItem('user');
|
||
|
|
}
|
||
|
|
|
||
|
|
checkAuth() {
|
||
|
|
const user = localStorage.getItem('user');
|
||
|
|
if (user) {
|
||
|
|
this.currentUser = JSON.parse(user);
|
||
|
|
this.isAuthenticated = true;
|
||
|
|
}
|
||
|
|
return this.isAuthenticated;
|
||
|
|
}
|
||
|
|
|
||
|
|
getUser() {
|
||
|
|
return this.currentUser;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const auth = new Auth();
|