mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-08 22:22:17 +02:00
add auth api service
This commit is contained in:
parent
c8fae413d2
commit
4d02c2eeed
3 changed files with 84 additions and 55 deletions
|
|
@ -1,55 +0,0 @@
|
||||||
export const login = async (request: {
|
|
||||||
username: string;
|
|
||||||
password: string;
|
|
||||||
grant_type?: string;
|
|
||||||
}) => {
|
|
||||||
const response = await fetch(
|
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/auth/jwt/login`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
username: request.username,
|
|
||||||
password: request.password,
|
|
||||||
grant_type: request.grant_type || "password",
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(data.detail || `HTTP ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const register = async (request: {
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
is_active: boolean;
|
|
||||||
is_superuser: boolean;
|
|
||||||
is_verified: boolean;
|
|
||||||
}) => {
|
|
||||||
const response = await fetch(
|
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/auth/register`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(data.detail || `HTTP ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
};
|
|
||||||
50
surfsense_web/lib/apis/auth/auth.service.ts
Normal file
50
surfsense_web/lib/apis/auth/auth.service.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import { LoginRequest, LoginResponse, RegisterRequest, RegisterResponse } from "./contracts";
|
||||||
|
|
||||||
|
export class AuthApiService {
|
||||||
|
login = async (request: LoginRequest) : Promise<LoginResponse> => {
|
||||||
|
const requestBody = new URLSearchParams();
|
||||||
|
requestBody.append("username", request.email);
|
||||||
|
requestBody.append("password", request.password);
|
||||||
|
requestBody.append("grant_type", "password");
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/auth/jwt/login`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
},
|
||||||
|
body: requestBody.toString(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(data.detail || `HTTP ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
register = async (request: RegisterRequest) : Promise<RegisterResponse> => {
|
||||||
|
const response = await fetch(
|
||||||
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/auth/register`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(request),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(data.detail || `HTTP ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
}
|
||||||
34
surfsense_web/lib/apis/auth/contracts.ts
Normal file
34
surfsense_web/lib/apis/auth/contracts.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
/**
|
||||||
|
* LOGIN
|
||||||
|
*/
|
||||||
|
export type LoginRequest = {
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
grant_type?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LoginResponse = {
|
||||||
|
access_token: string;
|
||||||
|
token_type: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* REGISTER
|
||||||
|
*/
|
||||||
|
export type RegisterRequest = {
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
is_active: boolean;
|
||||||
|
is_superuser: boolean;
|
||||||
|
is_verified: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RegisterResponse = {
|
||||||
|
id: number;
|
||||||
|
email: string;
|
||||||
|
is_active: boolean;
|
||||||
|
is_superuser: boolean;
|
||||||
|
is_verified: boolean;
|
||||||
|
pages_limit: number;
|
||||||
|
pages_used: number;
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue