#Discord OAuth on Nuxt

100 messages · Page 1 of 1 (latest)

tired vault
#

How would I go on with creating a Discord OAuth Login? Are there any examples? Should I use nuxt-appwrite or the appwrite-web package?

tired vault
tired vault
#

<@&1319717958645452800>?

errant citrus
#

Expanding on what is not working would be extremely helpful, I'm not a nuxt expert but if you can tell me what exactly isn't working I can try and help. Error messages, stack traces, etc are beneficial.

errant citrus
tired vault
#

Additionally I get a error 500 when loading the page:

500
useEvent is not defined

at setup ([REDACTED]/pages/account.js:7:21)
at _sfc_main.setup ([REDACTED]/pages/account.js:34:23)
at callWithErrorHandling ([REDACTED]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:200:19)
at setupStatefulComponent ([REDACTED]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7848:25)
at setupComponent ([REDACTED]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7809:36)
at renderComponentVNode ([REDACTED]/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:645:15)
at renderVNode ([REDACTED]/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:779:14)
at renderComponentSubTree ([REDACTED]/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:730:7)
at renderComponentVNode ([REDACTED]/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:664:12)
at renderVNode ([REDACTED]/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:779:14)
errant citrus
#

I'll forward this along to the appwrite team so they can be aware of the issues with the tutorial

tired vault
errant citrus
#

I'm wondering if they know there are issues since I'm not seeing this tutorial on the main tutorials page. Nevertheless it should be fixed or removed entirely imo.

tired vault
#

I hope that there is some way that it gets updates prayge

errant citrus
#

I think for the time being, you could just not use middleware, and check auth on each page. Remove any sort of typings. Lastly, fix the naming of the second occurance of oauth.post.js to oauth.get.js

tired vault
errant citrus
#

Here are the fixes I made that got it working

  1. fix the env reference in the server/lib/appwrite.ts file. It should be PUBLIC_APPWRITE_ENDPOINT, and PUBLIC_APPWRITE_PROJECT_ID. Also since these are undefined you need to have a check, so I just did || "" for now.

  2. Switch from using useEvent(), to using useRequestEvent()

Let me know if this helps any.

tired vault
#

Yes, that kind of works. Now I only have one more error from h3:

ℹ Error: Cannot set headers after they are sent to the client

 ⁃ at ServerResponse.setHeader (node:_http_outgoing:655:11)
 ⁃ at sendRedirect ([REDACTED]/node_modules/h3/dist/index.mjs:782:18)
 ⁃ at Object.handler ([REDACTED]/server/routes/index.js:6:1)

   1 ┃  export default defineEventHandler(async (event) => {
   2 ┃          if (event.context.user) {
   3 ┃                  await sendRedirect(event, "/account");
   4 ┃          }
   5 ┃
 ❯ 6 ┃          await sendRedirect(event, "/signup");
   7 ┃  });
   8 ┃

 ⁃ at runNextTicks (node:internal/process/task_queues:60:5)
 ⁃ at process.processImmediate (node:internal/timers:454:9)
 ⁃ at process.callbackTrampoline (node:internal/async_hooks:130:17)
 ⁃ (async file:///[REDACTED]/node_modules/h3/dist/index.mjs:2009:19)
 ⁃ at async Object.callAsync ([REDACTED]/node_modules/unctx/dist/index.mjs:72:16)
 ⁃ at async Server.toNodeHandle ([REDACTED]/node_modules/h3/dist/index.mjs:2301:7)

[CAUSE]
Error {
  stack: 'Cannot set headers after they are sent to the client\n' +
  '    at ServerResponse.setHeader (node:_http_outgoing:655:11)\n' +
  'at sendRedirect ([REDACTED]/node_modules/h3/dist/index.mjs:782:18)\n' +
  'at Object.handler ([REDACTED]/server/routes/index.js:6:1)\n' +
  '    at runNextTicks (node:internal/process/task_queues:60:5)\n' +
  '    at process.processImmediate (node:internal/timers:454:9)\n' +
  '    at process.callbackTrampoline (node:internal/async_hooks:130:17)\n' +
  'at async file:///[REDACTED]/node_modules/h3/dist/index.mjs:2009:19)\n' +
  'at async Object.callAsync ([REDACTED]/node_modules/unctx/dist/index.mjs:72:16)\n' +
  'at async Server.toNodeHandle ([REDACTED]/node_modules/h3/dist/index.mjs:2301:7)',
  message: 'Cannot set headers after they are sent to the client',
  code: 'ERR_HTTP_HEADERS_SENT',
}
errant citrus
#

hmmm yea, I honestly know nothing about that lol I'll see if I can find anything.

tired vault
#

Yeah, i'll try later if i can find a fix

errant citrus
#

This seemed to fix it for me

export default defineEventHandler(async (event) => {
  if (event.context.user) {
    return await sendRedirect(event, "/account");
  }

  return await sendRedirect(event, "/signin");
});
tired vault
#

Yeah, got it aswell, I just moved it into an else. But now (before aswell) the values pop up for a split second and then disappear followed by another error:

errant citrus
#

Yea, I think this error is because there is no type defined for the context

#

I think it has something to do with this

import type { Models } from "node-appwrite";

declare module "h3" {
  interface H3EventContext {
    user?: Models.User<Models.Preferences<any>>;
  }
}
#

You can try something like this

const event = useRequestEvent()
const user = event?.context?.user

Instead of doing object destructuring

tired vault
#

yeah, at least i dont get an error now.

#

but still the values disappear after the page fully loads

#

And when I logg out I get the following error:

500
User (role: guests) missing scope (account)

at _Client.call ([REDACTED]/esc/node_modules/node-appwrite/src/client.ts:354:9)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.handler ([REDACTED]/esc/server/api/signout.post.js:7:1)
at async file:///[REDACTED]/esc/node_modules/h3/dist/index.mjs:2009:19)
at async Object.callAsync ([REDACTED]/esc/node_modules/unctx/dist/index.mjs:72:16)
at async Server.toNodeHandle ([REDACTED]/esc/node_modules/h3/dist/index.mjs:2301:7)
errant citrus
#

really.. hmm.. I'm not running into that issue

tired vault
#

could be something with the sessions

errant citrus
#

Yea, it looks like your session is being removed for some reason

#

I forked the repo from above and made my changes to it, so maybe you could be missing something?

tired vault
#

could be?

tired vault
#

Does this make sense? Doesn't this need to be the url of my website?

errant citrus
#

What is the website URL you're using

errant citrus
tired vault
#

Thats event origin => localhost:3000

errant citrus
#

I believe it requires a protocol

#

http(s)://

tired vault
#

i think it includes http actually

errant citrus
#

can you check the console.log?

tired vault
errant citrus
#

does it need to be wrapped in ticks?

#
`${websiteUrl}/api/oauth`
tired vault
#

Yes, else the ${} wouldn't work

errant citrus
#

alright in the ss it wasn't so just verifying

tired vault
#

I even tried with a string directly but that didn't change anything

errant citrus
#

where is input coming from? Could the invalid URL not even be one of the two you put in but something else?

tired vault
#

wym?

#

What input?

errant citrus
tired vault
#

i dont think thats from me, only the "discord" from the provider

#

could it be the admin client not initializing correctly?

#

omg

#

i know why...

#

(my .env)

#

my appwrite.js

#

...

errant citrus
tired vault
#

yeah. that makes more sense now.

errant citrus
#

:P

tired vault
#

at least the other stuff should be fixed now

errant citrus
#

Cool beans, glad you're making some headway

tired vault
#

okay. i'm officially loosing my mind.

errant citrus
#

So you authenticated and now the context is empty of user data?

tired vault
#

the session is instantly clearing again

errant citrus
#

can you see what is triggering that?

tired vault
#

As soon as the page fully loads the content disappears

#

oohh

#

but that kind of proofs the exact opposite behavior

#

it's changing somewhere on the client for some reason. for the server it's still the old value tho

errant citrus
#

What does your code for this page loo like

tired vault
#
<!-- pages/account.vue -->
<script setup>
const event = useRequestEvent()
// console.log(event?.context)
const user = event?.context?.user
</script>

<template>
    <ul>
        <li>
            <strong>Email:</strong> {{ user?.email }}
        </li>
        <li>
            <strong>Name:</strong> {{ user?.name }}
        </li>
        <li>
            <strong>ID: </strong> {{ user?.$id }}
        </li>
    </ul>
    <form method="post" action="/api/signout">
        <button type="submit">Log out</button>
    </form>
</template>
#

the console log gives me the right user info tho

tired vault
errant citrus
#

try this

<script setup>
const event = useRequestEvent();
const user = useState("user", () => event?.context?.user);
</script>

<template>
    <ul>
        <li>
            <strong>Email:</strong> {{ user?.email }}
        </li>
        <li>
            <strong>Name:</strong> {{ user?.name }}
        </li>
        <li>
            <strong>ID: </strong> {{ user?.$id }}
        </li>
    </ul>
    <form method="post" action="/api/signout">
        <button type="submit">Log out</button>
    </form>
</template>
tired vault
errant citrus
#

Sorry I hadn't had the opportunity to look more into it

#

I'm not personally able to recreate the behavior you're experiencing so I'm not sure the best thing to do

#

Do you have any sort of error logs or anything?

tired vault
#

Seems like there is no error at all. The user object just returns undefined until reloading.

errant citrus
#

Hmm it seems nuxt isn’t taking into account the cookie values set until after reload. I’m not sure why, can you check in the middleware when it’s getting the user if that’s null or undefined? Maybe a console log?

tired vault
errant citrus
#

yea here log account and see if it's just undefined?

tired vault
#

Before reload:

auth middleware Account {
  client:
   _Client {
     config:
      { endpoint: '[REDACTED - APPWRITE_ENDPOINT]',
        selfSigned: false,
        project: '[REDACTED - APPWRITE_PROJECT]',
        key: '',
        jwt: '',
        locale: '',
        session: '',
        forwardeduseragent: '' },
     headers:
      { 'x-sdk-name': 'Node.js',
        'x-sdk-platform': 'server',
        'x-sdk-language': 'nodejs',
        'x-sdk-version': '15.0.1',
        'user-agent': 'AppwriteNodeJSSDK/15.0.1 (win32; x64) Node.js/v20.19.0',
        'X-Appwrite-Response-Format': '1.6.0',
        'X-Appwrite-Project': '[REDACTED - APPWRITE_PROJECT]' } } }

After reload:

        auth middleware Account {
  client:
   _Client {
     config:
      { endpoint: '[REDACTED - APPWRITE_ENDPOINT]',
        selfSigned: false,
        project: '[REDACTED - APPWRITE_PROJECT]',
        key: '',
        jwt: '',
        locale: '',
        session:
         'eyJpZCI6IjY3ZTZmMTVlOTY4NjVlYTZkNjZmIiwic2VjcmV0IjoiMTdlYjZiOTcxMGIzNTY5OTVhOWRlZjQzMmYwOTIyODhhNzYxNDZkZDk0NzIyNjU3ZThmOGQ0ZDgyZDk4NTVhODUxNmJlOWNlZWM5NTFhOWQwOWMzNGIwMTA3OGY0NGFhNmZmNDU3MzRhZjgwM2EyOTI5MDJlYTQ0ZDcyZjg4MWYzOTcxYTVlNTJkNGEwOTZhNmMzMDA4ZDdkZTQ4OWZkOWQ5MGJjOWNkZjI2NGJiMDcyMzQ2NjdlMTFmY2I2ZDZjNWMzYWJjMGNmZjZhMWU5NTk5MGE2ZTU1NGNlNWYzZDc4OTBkN2NlNjY5YzJkMjgwZWQ1NDY4YjI2YWUzYWRjZCJ9',
        forwardeduseragent: '' },
     headers:
      { 'x-sdk-name': 'Node.js',
        'x-sdk-platform': 'server',
        'x-sdk-language': 'nodejs',
        'x-sdk-version': '15.0.1',
        'user-agent': 'AppwriteNodeJSSDK/15.0.1 (win32; x64) Node.js/v20.19.0',
        'X-Appwrite-Response-Format': '1.6.0',
        'X-Appwrite-Project': '[REDACTED - APPWRITE_PROJECT]',
        'X-Appwrite-Session':
         'eyJpZCI6IjY3ZTZmMTVlOTY4NjVlYTZkNjZmIiwic2VjcmV0IjoiMTdlYjZiOTcxMGIzNTY5OTVhOWRlZjQzMmYwOTIyODhhNzYxNDZkZDk0NzIyNjU3ZThmOGQ0ZDgyZDk4NTVhODUxNmJlOWNlZWM5NTFhOWQwOWMzNGIwMTA3OGY0NGFhNmZmNDU3MzRhZjgwM2EyOTI5MDJlYTQ0ZDcyZjg4MWYzOTcxYTVlNTJkNGEwOTZhNmMzMDA4ZDdkZTQ4OWZkOWQ5MGJjOWNkZjI2NGJiMDcyMzQ2NjdlMTFmY2I2ZDZjNWMzYWJjMGNmZjZhMWU5NTk5MGE2ZTU1NGNlNWYzZDc4OTBkN2NlNjY5YzJkMjgwZWQ1NDY4YjI2YWUzYWRjZCJ9' } } }
#

seems like the createSessionClient doesn't even have anything in the event at that time

#

but oauth.get.js does.

tired vault
#

This makes no sense at all....

errant citrus
#

id suspect it's because it is not aware of the new cookie that was set on the server

#

i don't know what the fix would be

tired vault
#

clould be..

tired vault
errant citrus