2022-11-19 14:21:26 +00:00
|
|
|
// services
|
2023-01-26 18:12:20 +00:00
|
|
|
import APIService from "services/api.service";
|
2022-11-19 14:21:26 +00:00
|
|
|
|
|
|
|
const { NEXT_PUBLIC_API_BASE_URL } = process.env;
|
|
|
|
|
|
|
|
class AuthService extends APIService {
|
|
|
|
constructor() {
|
|
|
|
super(NEXT_PUBLIC_API_BASE_URL || "http://localhost:8000");
|
|
|
|
}
|
|
|
|
|
|
|
|
async emailLogin(data: any) {
|
2023-01-26 18:12:20 +00:00
|
|
|
return this.post("/api/sign-in/", data, { headers: {} })
|
2022-11-19 14:21:26 +00:00
|
|
|
.then((response) => {
|
|
|
|
this.setAccessToken(response?.data?.access_token);
|
|
|
|
this.setRefreshToken(response?.data?.refresh_token);
|
|
|
|
return response?.data;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async socialAuth(data: any) {
|
2023-01-26 18:12:20 +00:00
|
|
|
return this.post("/api/social-auth/", data, { headers: {} })
|
2022-11-19 14:21:26 +00:00
|
|
|
.then((response) => {
|
|
|
|
this.setAccessToken(response?.data?.access_token);
|
|
|
|
this.setRefreshToken(response?.data?.refresh_token);
|
|
|
|
return response?.data;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async emailCode(data: any) {
|
2023-01-26 18:12:20 +00:00
|
|
|
return this.post("/api/magic-generate/", data, { headers: {} })
|
|
|
|
.then((response) => response?.data)
|
2022-11-19 14:21:26 +00:00
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async magicSignIn(data: any) {
|
2023-01-26 18:12:20 +00:00
|
|
|
const response = await this.post("/api/magic-sign-in/", data, { headers: {} });
|
2022-11-19 14:21:26 +00:00
|
|
|
if (response?.status === 200) {
|
|
|
|
this.setAccessToken(response?.data?.access_token);
|
|
|
|
this.setRefreshToken(response?.data?.refresh_token);
|
|
|
|
return response?.data;
|
|
|
|
}
|
|
|
|
throw response.response.data;
|
|
|
|
}
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
async signOut() {
|
|
|
|
return this.post("/api/sign-out/", { refresh_token: this.getRefreshToken() })
|
2022-11-19 14:21:26 +00:00
|
|
|
.then((response) => {
|
|
|
|
this.purgeAccessToken();
|
|
|
|
this.purgeRefreshToken();
|
|
|
|
return response?.data;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.purgeAccessToken();
|
|
|
|
this.purgeRefreshToken();
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new AuthService();
|