forked from noxious/client
haha keyboard go brrr
This commit is contained in:
32
src/services/authService.ts
Normal file
32
src/services/authService.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import axios from 'axios';
|
||||
import config from '@/config';
|
||||
import { useSocketStore } from '@/stores/socket';
|
||||
|
||||
export async function register(username: string, password: string, socketStore = useSocketStore()) {
|
||||
if (socketStore.getAuthenticated) return console.log('User already authenticated');
|
||||
|
||||
try {
|
||||
const response = await axios.post(`${config.server_endpoint}/register`, { username, password });
|
||||
console.log(response.data);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error registering user:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function login(username: string, password: string, socketStore = useSocketStore()) {
|
||||
if (socketStore.getAuthenticated) return console.log('User already authenticated');
|
||||
|
||||
try {
|
||||
const response = await axios.post(`${config.server_endpoint}/login`, { username, password });
|
||||
if (response.status === 200) {
|
||||
socketStore.isAuthenticated = true;
|
||||
socketStore.setupSocketConnection(username, password);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user