kotanipay-sdk
Version:
Official Kotani Pay SDK for Node.js and Browser
70 lines • 2.06 kB
JavaScript
import { ValidationUtil } from '../../utils/validation.util';
export class AuthService {
constructor(httpClient) {
this.httpClient = httpClient;
}
/**
* Login with email - sends login link to email
*/
async login(email) {
if (!email) {
throw new Error('Email is required');
}
ValidationUtil.validateEmail(email);
const response = await this.httpClient.post('/api/v3/auth/login', { email });
return response;
}
/**
* Set session data after user clicks login link
*/
setSession(sessionData) {
this.session = sessionData;
// Update HTTP client to use the JWT token
this.httpClient.setAuthToken(sessionData.token);
}
/**
* Generate API key using the JWT token from login
*/
async generateApiKey(request) {
if (!this.session) {
throw new Error('No active session. Please login first and set session data.');
}
const response = await this.httpClient.get('/api/v3/auth/api-key/secure');
return response;
}
/**
* Refresh the JWT token using refresh token
*/
async refreshToken() {
if (!this.session?.refresh_token) {
throw new Error('No refresh token available. Please login again.');
}
const response = await this.httpClient.post('/api/v3/auth/refresh', { refresh_token: this.session.refresh_token });
this.session = response.data;
this.httpClient.setAuthToken(this.session.token);
return this.session;
}
/**
* Get current session data
*/
getSession() {
return this.session;
}
getTokenInfo() {
return this.session?.token;
}
/**
* Check if user is authenticated
*/
isAuthenticated() {
return !!this.session?.token;
}
/**
* Logout and clear session
*/
logout() {
this.session = undefined;
this.httpClient.clearAuthToken();
}
}
//# sourceMappingURL=auth.service.js.map