feat: /user info api

This commit is contained in:
Aravinth Manivannan 2023-09-18 19:06:17 +05:30
parent d2083b26ca
commit 7c5ae9b1e4
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 32 additions and 5 deletions

View file

@ -1,11 +1,23 @@
import Forgejo from "./api"; import Forgejo from "./api";
import authtoken from "../secrets/user1-accesstoken.json"; import authtoken from "../secrets/user1-accesstoken.json";
const token = authtoken["sha1"];
const username = authtoken["login"];
test("use authentication without setting it ", () => { test("use authentication without setting it ", () => {
const api = new Forgejo("http://localhost:3000", "owner_user"); const api = new Forgejo("http://localhost:3000", "owner_user");
const token = authtoken["sha1"]
expect(() => api.getTokenAuth()).toThrow(); expect(() => api.getTokenAuth()).toThrow();
api.setTokenAuth(token) api.setTokenAuth(token);
expect(api.getTokenAuth().getToken()).toBe(token) expect(api.getTokenAuth().getToken()).toBe(token);
let headers = api.getTokenAuthHeader();
expect(headers.Authorization).toBe(`token ${token}`);
});
test("verify /user API ", async () => {
const api = new Forgejo("http://localhost:3000", "owner_user");
api.setTokenAuth(token);
let user = await api.getUser();
console.log(user);
expect(user["login"]).toBe(username);
}); });

View file

@ -16,6 +16,7 @@ class Forgejo {
*/ */
constructor(url: string, username: string) { constructor(url: string, username: string) {
this.url = new URL(url); this.url = new URL(url);
console.log(this.url.toString());
this.username = username; this.username = username;
} }
@ -25,7 +26,6 @@ class Forgejo {
*/ */
setTokenAuth(token: string) { setTokenAuth(token: string) {
this.token = new Auth(token); this.token = new Auth(token);
console.log(this.token.getToken())
} }
getTokenAuth(): Auth { getTokenAuth(): Auth {
@ -37,7 +37,22 @@ class Forgejo {
} }
getTokenAuthHeader() { getTokenAuthHeader() {
return { Authentication: `Bearer ${this.getTokenAuth().getToken()}` }; return { Authorization: `token ${this.getTokenAuth().getToken()}` };
}
/**
* Get logged in user
*/
async getUser() {
this.url.pathname = "/api/v1/user";
console.log(this.url.toString());
let res = await fetch(this.url, {
method: "GET",
credentials: "include",
headers: this.getTokenAuthHeader(),
});
console.log(`got res: ${res}`);
return await res.json();
} }
/** /**