Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

auth-test.tsx 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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((sponsor: { avatar: string }) =>
  57. sponsor.avatar.includes(id)
  58. )
  59. console.log(
  60. JSON.stringify({
  61. user: session.user,
  62. sponsors: sponsors.map((sponsor: any) => sponsor.avatar),
  63. isSponsor: sponsor !== undefined,
  64. })
  65. )
  66. isSponsor = sponsor !== undefined
  67. }
  68. return {
  69. props: {
  70. isOwner: session?.user?.image.includes('23072548') || false,
  71. isSponsor,
  72. ssrSession: session,
  73. },
  74. }
  75. }