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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-16 13:30:04 +00:00
|
|
|
async emailSignUp(data: { email: string; password: string }) {
|
|
|
|
return this.post("/api/sign-up/", data, { headers: {} })
|
|
|
|
.then((response) => {
|
|
|
|
this.setAccessToken(response?.data?.access_token);
|
|
|
|
this.setRefreshToken(response?.data?.refresh_token);
|
|
|
|
return response?.data;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
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();
|