Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten committed Aug 10, 2023
1 parent 9206a45 commit a88db0c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
13 changes: 9 additions & 4 deletions pages/repos/add.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
</div>

<div v-else class="mx-auto flex flex-col items-center max-w-2xl">
<div v-if="selectedForge">
<span class="text-xl font-bold">{{ selectedForge?.name }} - {{ selectedForge.host }}</span>
<div @click="selectedForgeId = undefined">x</div>
</div>

<TextInput
:model-value="search"
placeholder="Search for a repo ..."
Expand All @@ -40,7 +45,11 @@
<script setup lang="ts">
const loading = ref(false);
const { user } = useAuth();
const { data: forges } = await useFetch('/api/user/forges', {
server: false,
});
const selectedForgeId = ref<number>();
const selectedForge = computed(() => forges.value?.find((f) => f.id === selectedForgeId.value));

const search = ref(`user:${user.value?.login}`);
const { data: repositories } = await useAsyncData(
Expand All @@ -57,10 +66,6 @@ const { data: repositories } = await useAsyncData(
},
);
const { data: forges } = await useFetch('/api/user/forges', {
server: false,
});
function debounce<T extends Function>(cb: T, wait = 20) {
let h = 0;
let callable = (...args: any) => {
Expand Down
10 changes: 5 additions & 5 deletions server/api/forges/[forge_id]/search.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default defineEventHandler(async (event) => {
});
}

const user = await getUserFromCookie(event);
if (!user) {
throw new Error('User not found');
}

const search = ((getQuery(event)?.search as string | undefined) || '').trim();

const config = useRuntimeConfig();
Expand All @@ -31,11 +36,6 @@ export default defineEventHandler(async (event) => {
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

const user = await getUserFromCookie(event);
if (!user) {
throw new Error('User not found');
}

const forge = await getUserForgeAPI(user, parseInt(forgeId, 10));

const userRepos = await forge.getRepos(search);
Expand Down
4 changes: 3 additions & 1 deletion server/api/forges/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import { forgeSchema } from '../../schemas';

export default defineEventHandler(async (event) => {
const forges = await db.select().from(forgeSchema).where(eq(forgeSchema.allowLogin, true)).all();
return forges;

// use map to hide client id and secret
return forges.map((forge) => ({ id: forge.id, name: forge.name, host: forge.host, type: forge.type }));
});
21 changes: 5 additions & 16 deletions server/forges/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { H3Event } from 'h3';
import { Gitlab } from './gitlab';
import { Forge as ForgeModel } from '../schemas';
import { Github } from './github';
import { Credentials, Forge, ForgeUser, Repo, Tokens, UserWithTokens } from './types';
import jwt from 'jsonwebtoken'; // TODO: fix type
import jwtDecode from 'jwt-decode';

const jwtSecret = '123'; // TODO: load from nuxt settings
const jwtSecret = '123456789'; // TODO: move to nuxt settings

class ForgeApi {
private user: UserWithTokens;
Expand All @@ -16,23 +15,13 @@ class ForgeApi {
this.forge = forge;
}

private async verifyJWT<T>(token: string, secret: string): Promise<T> {
return new Promise<T>((resolve, reject) => {
jwt.verify(token, secret, (err, decoded) => {
if (err) {
reject(err);
} else {
resolve(decoded);
}
});
});
}

private async refreshTokenIfNeeded(user: UserWithTokens): Promise<Tokens> {
const decoded = await this.verifyJWT<{ exp: number }>(user.tokens.accessToken, jwtSecret);
const decoded = await jwtDecode<{ exp: number }>(user.tokens.accessToken);
if (decoded.exp * 1000 > Date.now()) {
return user.tokens;
}

// refresh token
const newTokens = await this.forge.refreshToken(user.tokens.refreshToken);
user.tokens = newTokens;

Expand Down
2 changes: 2 additions & 0 deletions server/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ export async function getUserForgeAPI(user: User, forgeId: number) {
refreshToken: userForge.refreshToken,
};

console.log('tokens', tokens); // TODO: check tokens

return getForgeApiFromDB({ ...user, tokens }, forgeModel);
}

0 comments on commit a88db0c

Please sign in to comment.