Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magic.Link added as provider to @redwoodjs/auth #562

Merged
merged 27 commits into from
May 28, 2020
Merged

Magic.Link added as provider to @redwoodjs/auth #562

merged 27 commits into from
May 28, 2020

Conversation

BurnedChris
Copy link
Contributor

Hello,

I am currently have added Magic.Link as a provider following there SDK outlined here.

I have not tested it yet as I have an issue #561 finding redwood-tools

@BurnedChris
Copy link
Contributor Author

The build and lint tests also fail as by the error of error TS2307: Cannot find module 'react-native'. I suspect this is because magic-js is a cross-platform sdk that supports React, React Native and React Native Webview. Any ideas how we would work around this problem?

@BurnedChris
Copy link
Contributor Author

BurnedChris commented May 20, 2020

The crooks of the implementation is done. 🚀

Thank you to @peterp for all the help!

You can follow how i have implemented it into the invoice app here

Main files -
Redwood Provider - here
API - auth.ts - here

Things to note

On the API side i though it was best that in that the magic-admin client was loaded in the developers project instead of inside of redwood, how does that effect everything?
AuthHeader.ts

case 'magicLinks': {
      decoded = token
      break
    }

the token is just handed back to the client to decode inside of Auth.lib As seen Below


import { db } from 'src/lib/db'
import { AuthenticationError } from '@redwoodjs/api'
import { Magic } from '@magic-sdk/admin'

const mAdmin = new Magic(process.env.MAGICLINK_SECRET)

export const getCurrentUser = async (authToken) => {
  const { email, publicAddress } = await mAdmin.users.getMetadataByToken(
    authToken
  )

  if (!email || !publicAddress) {
    throw new AuthenticationError('Uh, oh')
  }

  let user = await db.user.findOne({ where: { email } })
  if (!user) {
    user = await db.user.create({ data: { email, publicAddress } })
  }
  return user
}

Current Problems

  • currentUser returns instead of {issuer, email, publicAddress} sdk
  • Create Documentation on how to add magiclinks into your rw project
  • Add correct Types in AuthClient

@peterp
Copy link
Contributor

peterp commented May 21, 2020

Wooh! This is cool, and I think the way that you've done it makes a lot of sense. We don't really want to bundle esoteric decoding libraries with @redwoodjs/api

Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great. I would love to have the type definitions working, but otherwise I think this can be merged!

I made a suggestion about the name of the provider.

packages/api/src/auth/authHeaders.ts Outdated Show resolved Hide resolved
packages/api/src/auth/authHeaders.ts Outdated Show resolved Hide resolved
packages/auth/src/authClient.ts Outdated Show resolved Hide resolved
packages/auth/src/authClient.ts Outdated Show resolved Hide resolved
burnsy and others added 4 commits May 21, 2020 09:26
changed naming from magicLinks to magic.link

Co-authored-by: Peter Pistorius <peter.pistorius@gmail.com>
changed naming from magicLinks to magic.link

Co-authored-by: Peter Pistorius <peter.pistorius@gmail.com>
changed naming from magicLinks to magic.link

Co-authored-by: Peter Pistorius <peter.pistorius@gmail.com>
changed naming from magicLinks to magic.link

Co-authored-by: Peter Pistorius <peter.pistorius@gmail.com>
* refs/heads/pp-auth-magic-links:
  Linted code
@BurnedChris
Copy link
Contributor Author

I agree with the name change, will work on the other bits tonight i have opened a comment on magics SDK asking for advice with the typing.

@smithki
Copy link

smithki commented May 21, 2020

Hey, just wanted to mention that I think we've solved your problem in magic-sdk@1.3.4.

You should be able to type the instance just as you would expect now:

import { Magic } from 'magic-sdk';

let foo: Magic; // foo will equal `InstanceType<typeof Magic>`
foo = new Magic(...); // ✨ works!

Please let me know if other blockers arise!

@peterp
Copy link
Contributor

peterp commented May 21, 2020

@smithki AMAZING! Thank you!

Copy link
Contributor Author

@BurnedChris BurnedChris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Causes one error on the logout.

packages/auth/src/authClient.ts Outdated Show resolved Hide resolved
@BurnedChris
Copy link
Contributor Author

@smithki it looks like everything is typed correctly. But it looks like we are still having the build errors coming from react-native types inside of react-native-webview.

##[error]../../node_modules/react-native-webview/lib/WebViewTypes.d.ts(2,140): error TS2307: Cannot find module 'react-native'.

@smithki
Copy link

smithki commented May 21, 2020

I'll pull your branch locally and see if I can figure out why. This is the first I've seen such an issue 🤔

@smithki
Copy link

smithki commented May 22, 2020

@burnsy -- I think I've solved the issue in magic-sdk@1.3.5. Please let me know if the issue persists. Happy to take another look.

Here's more context around what the issue boils down to:

We support web and React Native environments, but we have no way of signalling to TypeScript that we don't want RN typings to pollute the web bundle. It's very difficult to separate the definitions without resorting to hacks. Honestly it's sort of mind-boggling that it's not a baked-in feature of TypeScript, but at least there's a relevant issue open: microsoft/TypeScript#18588

Christopher Burns added 4 commits May 22, 2020 10:11
… into HEAD

* 'pp-auth-magic-links' of https://github.com/Burnsy/redwood: (22 commits)
  create option default using check for API and Web existing
  Make `db` optional.
  Clean up messages.
  Add tests.
  Add more type information for sides.
  Remove "args" package.
  Fix types.
  Fix docs.
  Update readme docs.
  Install deps.
  Add missing declarations.
  Refactor the dev-server.
  Add node and browser path types.
  Run yarn lint --fix
  Check for QUERY export before ignoring a Cell file with default export
  Allow non-cell files name end with Cell
  Add IP address to the Lambda event's requestContext
  Update packages/web/src/graphql/withCell.js
  Update packages/web/src/graphql/withCell.js
  Update packages/web/src/graphql/withCell.js
  ...
@BurnedChris BurnedChris changed the title [wip] Magic.Link added as provider to @redwoodjs/auth Magic.Link added as provider to @redwoodjs/auth May 22, 2020
@BurnedChris
Copy link
Contributor Author

I believe thats everything! I have wrote some documentation on how to integrate it.
It may need tweaking around the API Specific Integration as i changed the not about auth0 into a section with magic link under it so it flows a bit better than two notes.

Thanks for all your help @peterp and @smithki 🎉

@peterp
Copy link
Contributor

peterp commented May 22, 2020

Thanks for this @burnsy, you legend! Did you get some stickers?

@BurnedChris
Copy link
Contributor Author

BurnedChris commented May 22, 2020

@peterp Yes, I would love to see something similar to Gatsby! Redwood definitely needs a small e-commerce store. I would love to get a redwood T-shirt or something!

@peterp peterp added this to the next release milestone May 28, 2020
@peterp peterp merged commit 4d678d8 into redwoodjs:master May 28, 2020
@BurnedChris BurnedChris deleted the pp-auth-magic-links branch May 28, 2020 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants