N
Nuxt8mo ago
netwrx

No session created - nuxt auth utils

export default oauth.discordEventHandler({
config: {
emailRequired: false,
profileRequired: true,
scope: ["guilds"],
},
async onSuccess(event, { user, tokens }) {
console.log(user.username);
await setUserSession(event, {
user: {
username: user.username,
},
});
return sendRedirect(event, "/");
},
// Optional, will return a json error and 401 status code by default
onError(event, error) {
console.error("Discord OAuth error:", error);
return sendRedirect(event, "/");
},
});
export default oauth.discordEventHandler({
config: {
emailRequired: false,
profileRequired: true,
scope: ["guilds"],
},
async onSuccess(event, { user, tokens }) {
console.log(user.username);
await setUserSession(event, {
user: {
username: user.username,
},
});
return sendRedirect(event, "/");
},
// Optional, will return a json error and 401 status code by default
onError(event, error) {
console.error("Discord OAuth error:", error);
return sendRedirect(event, "/");
},
});
No cookies, and no session is set after a successful auth. Even logging the session directly afterwards shows good data and it was set. But nada.
27 Replies
netwrx
netwrxOP8mo ago
I also get weird behavior with the data returned, it includes an email even though I'm not authorizing an email scope (neither on the server or discord's side has it set) This doesn't help me. I don't want to do it myself Didn’t mean to sound passive aggressive. I just want this module to work, if it doesn’t then I’ll just move on I’m not sure if I’m doing something wrong on my side or what
🖤 SHADXW 🖤
Here is how I do it.
export default defineNuxtRouteMiddleware(async (to, from) => {
const userStore = useUserStore();

interface AuthData {
auth: boolean;
}

const { data } = await useAsyncData('auth', async () => {
const { data } = await useFetch<AuthData>('/api/auth', {
credentials: 'include',
});
return data.value;
});

if (data.value) {
userStore.$patch({ auth: data.value.auth });
}

if (!userStore.auth) {
return navigateTo({ path: '/login' });
}
});
export default defineNuxtRouteMiddleware(async (to, from) => {
const userStore = useUserStore();

interface AuthData {
auth: boolean;
}

const { data } = await useAsyncData('auth', async () => {
const { data } = await useFetch<AuthData>('/api/auth', {
credentials: 'include',
});
return data.value;
});

if (data.value) {
userStore.$patch({ auth: data.value.auth });
}

if (!userStore.auth) {
return navigateTo({ path: '/login' });
}
});
Might give you some help I hope
netwrx
netwrxOP8mo ago
I appreciate the contribution here, but I actually moved on to using https://lucia-auth.com
Lucia
Lucia documentation
Lucia is an open source auth library that abstracts away the complexity of handling sessions.
netwrx
netwrxOP8mo ago
It's really good, more powerful than nuxt-auth-utils
Hum+
Hum+8mo ago
I think I found your problem
const redirectUrl = getRequestURL(event).href
const redirectUrl = getRequestURL(event).href
In local dev will return: http://localhost:3000/auth/discord even when coming back off https resulting in a invalid redirect uri error on discord side you have to include the localhost:3000 on the discord dashboard to
Hum+
Hum+8mo ago
No description
Hum+
Hum+8mo ago
which will fix this local dev side issue and in bare essence just work
No description
Hum+
Hum+8mo ago
I helped rewrite the provider for discord and linkedin on nuxt auth utils, i read you already moved on to lucia, which is also a nice way of doing it if you wanna extend the user object beyond the name, you def will need to dive into the discord scopes the reason for scoping email without submitting is this piece of code:
export default oauth.discordEventHandler({
config: {
emailRequired: true // <--
},
...
export default oauth.discordEventHandler({
config: {
emailRequired: true // <--
},
...
&
const redirectUrl = getRequestURL(event).href;
if (!code) {
config.scope = config.scope || [];
if (config.emailRequired && !config.scope.includes("email")) {
config.scope.push("email");
}
if (config.profileRequired && !config.scope.includes("identify")) {
config.scope.push("identify");
}
return sendRedirect(
event,
withQuery(config.authorizationURL, {
response_type: "code",
client_id: config.clientId,
redirect_uri: redirectUrl,
scope: config.scope.join(" "),
...config.authorizationParams
})
);
const redirectUrl = getRequestURL(event).href;
if (!code) {
config.scope = config.scope || [];
if (config.emailRequired && !config.scope.includes("email")) {
config.scope.push("email");
}
if (config.profileRequired && !config.scope.includes("identify")) {
config.scope.push("identify");
}
return sendRedirect(
event,
withQuery(config.authorizationURL, {
response_type: "code",
client_id: config.clientId,
redirect_uri: redirectUrl,
scope: config.scope.join(" "),
...config.authorizationParams
})
);
if you dont want the email to be appended set:
export default oauth.discordEventHandler({
config: {
emailRequired: false
},
...
export default oauth.discordEventHandler({
config: {
emailRequired: false
},
...
and it will only scope in 'identify' which is mandatory, then follow the normal route for extending the user object.
netwrx
netwrxOP8mo ago
That didn’t happen. URI was correct and did redirect correctly Everything worked besides the session
manniL
manniL8mo ago
just to double check, you do use SSR and not noly SSG right?
netwrx
netwrxOP8mo ago
Yes always
Hum+
Hum+8mo ago
im down to help you out if interested lucia is great too but imo nuxt auth utils should be enough
netwrx
netwrxOP8mo ago
Not anymore, I appreciate the contribution to the thread tho Hopefully it helps someone else
Hum+
Hum+8mo ago
I do feel interested to explore the problem you had with it, could you help me with a one liner describing where and when the issue happened for you? Is it when you add custom data to the user/session object? what I have now is: - no session being stored - and second you had trouble with the extended user object?
netwrx
netwrxOP8mo ago
^
Hum+
Hum+8mo ago
did it set: username at all for you?
netwrx
netwrxOP8mo ago
No. Nuxt dev tools also shown session was always null No matter what
Hum+
Hum+8mo ago
thank you, ima dive into it i tried setting up a clean repo yesterday to reproduce your error
netwrx
netwrxOP8mo ago
I think it works better personally especially how easy it is to integrate different types of databases and it has good http safety
Hum+
Hum+8mo ago
lucia is fun ngl, i tried v2, fell in love with it, then migrated to v3 which im currently fighting
netwrx
netwrxOP8mo ago
If you got issues their discord could be a great place to ask
Hum+
Hum+8mo ago
im there too 😛
netwrx
netwrxOP8mo ago
Oh, just let me know!
Hum+
Hum+8mo ago
some nice fellow over there helped me with a working drizzle/ luciav2 nuxt repo on github i took his repo and updated it to the experimental db0 version and migrated to lucia v3 but having issues https://github.com/justserdar/jsd-nuxt-turso-drizzle, main is fine, i got the issues in nightly+lucia branch
netwrx
netwrxOP8mo ago
Feel free to create a thread there. There are a few nuxt nerds there including me
Hum+
Hum+8mo ago
I might do that actually
Rudo Kemper
Rudo Kemper3mo ago
I seem to be having the same issue as described here, and would like to solve it (keeping with nuxt-auth-utils instead of switching to a different library) https://github.com/atinux/nuxt-auth-utils/issues/165
GitHub
Sessions not being set after successful authentication (loggedIn ...
I am having an issue that others seem to have encountered before (cf. this Discord thread or perhaps this issue), but have not been able to figure out a solution: Sessions are not being set after s...
Want results from more Discord servers?
Add your server