Skip to content

Commit

Permalink
fix: redirects after login
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Oct 11, 2024
1 parent 1ddd559 commit db164c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default defineNuxtConfig({
compatibilityDate: '2024-07-01',
runtimeConfig: {
serverUrl: 'https://dash.globalping.io',
public: {
gpAuthUrl: process.env.GP_API_URL || 'https://auth.globalping.io',
directusUrl: process.env.DIRECTUS_URL || 'https://dash-directus.globalping.io',
Expand Down Expand Up @@ -53,6 +54,7 @@ export default defineNuxtConfig({
},
$development: {
runtimeConfig: {
serverUrl: 'http://localhost:13010',
public: {
gpAuthUrl: process.env.GP_API_URL || 'http://localhost:13110',
directusUrl: process.env.DIRECTUS_URL || 'http://localhost:18055',
Expand Down
13 changes: 13 additions & 0 deletions server/routes/auth/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
const serverRootUrl = new URL(config.serverUrl);
const query = getQuery(event);
const redirect = query.redirect?.toString() || '/';
let redirectUrl = new URL(redirect, config.serverUrl);

if (redirectUrl.origin !== serverRootUrl.origin) {
redirectUrl = serverRootUrl;
}

await sendRedirect(event, redirectUrl.toString());
});
3 changes: 2 additions & 1 deletion store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export const useAuth = defineStore('auth', {
async login () {
const config = useRuntimeConfig();
const redirect = this.getRedirectUrl();
await navigateTo(`${config.public.directusUrl}/auth/login/github?redirect=${encodeURIComponent(redirect)}`, { external: true });
const directusRedirect = `${location.origin}/auth/callback?redirect=${encodeURIComponent(redirect.toString())}`;
await navigateTo(`${config.public.directusUrl}/auth/login/github?redirect=${encodeURIComponent(directusRedirect)}`, { external: true });
},
async logout () {
const { $directus } = useNuxtApp();
Expand Down

0 comments on commit db164c5

Please sign in to comment.