We publish them now, so let's use them in cvmts! Additionally, this removes the 'shared' module entirely, since it has little purpose anymore. The logger is replaced with pino (because superqemu uses pino for logging itself).
43 lines
979 B
TypeScript
43 lines
979 B
TypeScript
import { Rank, User } from './User.js';
|
|
|
|
export default class AuthManager {
|
|
apiEndpoint: string;
|
|
secretKey: string;
|
|
|
|
constructor(apiEndpoint: string, secretKey: string) {
|
|
this.apiEndpoint = apiEndpoint;
|
|
this.secretKey = secretKey;
|
|
}
|
|
|
|
async Authenticate(token: string, user: User): Promise<JoinResponse> {
|
|
let response = await fetch(this.apiEndpoint + '/api/v1/join', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
secretKey: this.secretKey,
|
|
sessionToken: token,
|
|
ip: user.IP.address
|
|
})
|
|
});
|
|
|
|
// Make sure the fetch returned okay
|
|
if (!response.ok) throw new Error(`Failed to query auth server: ${response.statusText}`);
|
|
|
|
let json = (await response.json()) as JoinResponse;
|
|
|
|
if (!json.success) throw new Error(json.error);
|
|
|
|
return json;
|
|
}
|
|
}
|
|
|
|
interface JoinResponse {
|
|
success: boolean;
|
|
clientSuccess: boolean;
|
|
error: string | undefined;
|
|
username: string | undefined;
|
|
rank: Rank;
|
|
}
|