thgh / payload-plugin-oauth

Add oAuth sign in to your Payload CMS site
MIT License
89 stars 16 forks source link

Feature for updating user info on signin #22

Closed augustgaukstad closed 6 months ago

augustgaukstad commented 6 months ago

I have a case where I would love it if my Payload-users get updated data when they sign in.

Currently got a flow for this working locally like this:

if (users.docs && users.docs.length) {
    user = users.docs[0];
    user = await payload.update({
        collection: collectionSlug,
        id: user.id,
        data: info,
        showHiddenFields: true,
    })
    user.collection = collectionSlug;
    user._strategy = 'oauth2';
}

The info contains up to date data fetched through the authorization flow.

Tried cloning the repo and replicating my code, but getting a type-error on this line: id: user.id

Error: Property 'id' does not exist on type 'User & { collection?: any; _strategy?: any; }'.

Guessing it's because there actually isn't a User collection with values being referenced? Tried addind id: number to the User interface, but that creates a whole bunch of other type issues.

If anyone has any ideas on how to make this work I think it'd be a great addition to the plugin :)

thgh commented 6 months ago

Looks like the type expects id: string | number which doesn't error

-interface User {}
+interface User { id: string | number }

https://github.com/thgh/payload-plugin-oauth/blob/e0a6b6e71d7acf56d5fc76b5e91fa1513f0eeacb/src/index.ts#L25

augustgaukstad commented 6 months ago

Thanks! That did the trick.

I'm trying to follow the contribution guide from the readme, but don't seem to have access to push to this repo. VSCode suggests creating a fork and pushing to that, is that the correct workflow? First time contributing to someone else's repo, sorry for the noobyness :)

Edit: i did create a new branch for the feature btw, assumed that would be best practice

thgh commented 6 months ago

Congrats on your first contribution (in progress) 😛

Yes, typically one follows these steps:

  1. Fork the repo: https://github.com/thgh/payload-plugin-oauth/fork
  2. Clone the fork
  3. Commit & push changes to the fork
  4. Then a pull request button appears over here: https://github.com/thgh/payload-plugin-oauth/pulls
thgh commented 6 months ago

Another tip: you can write "Resolve #22" in the PR message, which will tell Github to close this issue after the PR is merged.