N
Nuxt4mo 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.
26 Replies
netwrx
netwrx4mo 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
netwrx4mo 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
netwrx4mo ago
It's really good, more powerful than nuxt-auth-utils
Hum+
Hum+4mo 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+4mo ago
No description
Hum+
Hum+4mo ago
which will fix this local dev side issue and in bare essence just work
No description
Hum+
Hum+4mo 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
netwrx4mo ago
That didn’t happen. URI was correct and did redirect correctly Everything worked besides the session
manniL
manniL4mo ago
just to double check, you do use SSR and not noly SSG right?
netwrx
netwrx4mo ago
Yes always
Hum+
Hum+4mo ago
im down to help you out if interested lucia is great too but imo nuxt auth utils should be enough
netwrx
netwrx4mo ago
Not anymore, I appreciate the contribution to the thread tho Hopefully it helps someone else
Hum+
Hum+4mo 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
netwrx4mo ago
^
Hum+
Hum+4mo ago
did it set: username at all for you?
netwrx
netwrx4mo ago
No. Nuxt dev tools also shown session was always null No matter what
Hum+
Hum+4mo ago
thank you, ima dive into it i tried setting up a clean repo yesterday to reproduce your error
netwrx
netwrx4mo 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+4mo ago
lucia is fun ngl, i tried v2, fell in love with it, then migrated to v3 which im currently fighting
netwrx
netwrx4mo ago
If you got issues their discord could be a great place to ask
Hum+
Hum+4mo ago
im there too 😛
netwrx
netwrx4mo ago
Oh, just let me know!
Hum+
Hum+4mo 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
netwrx4mo ago
Feel free to create a thread there. There are a few nuxt nerds there including me
Hum+
Hum+4mo ago
I might do that actually
Want results from more Discord servers?
Add your server
More Posts