my_book_list_front/src/services/authentication.service.ts

31 lines
731 B
TypeScript

import type { User } from "@/models/user";
import axios from "axios";
export const MBL_API_URL = "http://localhost:8000/api/";
export class AuthenticationService {
constructor() {
axios.defaults.baseURL = `${MBL_API_URL}`
}
async findUsers() {
return await axios.get('users').then((res) => {
return res.data as User
})
}
async authenticate(username: string, password: string) {
return await axios
.post('token/', {
username: username,
password: password,
})
.then((res) => {
localStorage.setItem('refresh', res.data.refresh)
sessionStorage.setItem('access', res.data.access)
})
.catch((error) => {
throw error;
})
}
}