Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

auth-test.tsx 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Head from 'next/head'
  2. import { signIn, signOut, getSession, useSession } from 'next-auth/client'
  3. import { GetServerSidePropsContext } from 'next'
  4. export default function Home({
  5. ssrSession,
  6. isOwner,
  7. isSponsor,
  8. }: {
  9. isOwner: boolean
  10. isSponsor: boolean
  11. ssrSession: any
  12. }) {
  13. const [session, loading] = useSession()
  14. return (
  15. <>
  16. <Head>
  17. <title>tldraw</title>
  18. </Head>
  19. <div>
  20. <button onClick={() => signIn()}>Sign In</button>
  21. <button onClick={() => signOut()}>Sign Out</button>
  22. <p>{loading && 'Loading...'}</p>
  23. <pre>{JSON.stringify(session, null, 2)}</pre>
  24. <p>Is owner? {isOwner.toString()}</p>
  25. <p>Is sponsor? {isSponsor.toString()}</p>
  26. {isSponsor ? (
  27. <p>
  28. <b>Hey, thanks for sponsoring me!</b>
  29. </p>
  30. ) : (
  31. <p>
  32. <b>
  33. This site is just for my github sponsors.{' '}
  34. <a
  35. href="https://github.com/sponsors/steveruizok"
  36. target="_blank"
  37. rel="noopener noreferrer"
  38. >
  39. Sponsor here!
  40. </a>
  41. </b>
  42. </p>
  43. )}
  44. </div>
  45. </>
  46. )
  47. }
  48. export async function getServerSideProps(context: GetServerSidePropsContext) {
  49. let isSponsor = false
  50. const session = await getSession(context)
  51. if (session?.user) {
  52. const id = session.user.image.match(/u\/(.*)\?/)?.[1]
  53. const sponsors = await fetch(
  54. 'https://sponsors.trnck.dev/sponsors/steveruizok'
  55. ).then((d) => d.json().then((d) => d.sponsors))
  56. const sponsor = sponsors.find(
  57. (sponsor: { avatar: string }) => sponsor.avatar === session.user.image
  58. )
  59. isSponsor = sponsor !== undefined
  60. }
  61. return {
  62. props: {
  63. isOwner: session.user.email === 'steveruizok@gmail.com',
  64. isSponsor,
  65. ssrSession: session,
  66. },
  67. }
  68. }