UserManagement.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { useParams } from 'common'
  2. import CodeSnippet from '../CodeSnippet'
  3. import { DocSection } from '../DocSection'
  4. import Snippets from '../Snippets'
  5. import { InlineLink } from '@/components/ui/InlineLink'
  6. import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
  7. import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
  8. import { DOCS_URL } from '@/lib/constants'
  9. import { makeRandomString } from '@/lib/helpers'
  10. const randomPassword = makeRandomString(20)
  11. interface UserManagementProps {
  12. selectedLang: 'bash' | 'js'
  13. showApiKey: string
  14. }
  15. export const UserManagement = ({ selectedLang, showApiKey }: UserManagementProps) => {
  16. const { ref: projectRef } = useParams()
  17. const keyToShow = showApiKey ? showApiKey : 'BRIVEN_KEY'
  18. const { authenticationSignInProviders } = useIsFeatureEnabled([
  19. 'authentication:sign_in_providers',
  20. ])
  21. const { data: settings } = useProjectSettingsV2Query({ projectRef })
  22. const protocol = settings?.app_config?.protocol ?? 'https'
  23. const hostEndpoint = settings?.app_config?.endpoint ?? ''
  24. const endpoint = `${protocol}://${hostEndpoint ?? ''}`
  25. return (
  26. <div className="flex flex-col flex-1">
  27. <DocSection
  28. title="User Management"
  29. content={
  30. <>
  31. <p>Briven makes it easy to manage your users.</p>
  32. <p>
  33. Briven assigns each user a unique ID. You can reference this ID anywhere in your
  34. database. For example, you might create a <code>profiles</code> table that references
  35. the user using a <code>user_id</code> field.
  36. </p>
  37. <p>
  38. Briven already has built in the routes to sign up, login, and log out for managing
  39. users in your apps and websites.
  40. </p>
  41. </>
  42. }
  43. />
  44. <DocSection
  45. title="Sign up"
  46. content={
  47. <>
  48. <p>Allow your users to sign up and create a new account.</p>
  49. <p>
  50. After they have signed up, all interactions using the Briven JS client will be
  51. performed as "that user".
  52. </p>
  53. </>
  54. }
  55. snippets={
  56. <CodeSnippet
  57. selectedLang={selectedLang}
  58. snippet={Snippets.authSignup(endpoint, keyToShow, randomPassword)}
  59. />
  60. }
  61. />
  62. <DocSection
  63. title="Log in with Email/Password"
  64. content={
  65. <>
  66. <p>If an account is created, users can login to your app.</p>
  67. <p>
  68. After they have logged in, all interactions using the Briven JS client will be
  69. performed as "that user".
  70. </p>
  71. </>
  72. }
  73. snippets={
  74. <CodeSnippet
  75. selectedLang={selectedLang}
  76. snippet={Snippets.authLogin(endpoint, keyToShow, randomPassword)}
  77. />
  78. }
  79. />
  80. <DocSection
  81. title="Log in with Magic Link via Email"
  82. content={
  83. <>
  84. <p>Send a user a passwordless link which they can use to redeem an access_token.</p>
  85. <p>
  86. After they have clicked the link, all interactions using the Briven JS client will
  87. be performed as "that user".
  88. </p>
  89. </>
  90. }
  91. snippets={
  92. <CodeSnippet
  93. selectedLang={selectedLang}
  94. snippet={Snippets.authMagicLink(endpoint, keyToShow)}
  95. />
  96. }
  97. />
  98. <DocSection
  99. title="Sign Up with Phone/Password"
  100. content={
  101. <>
  102. <p>
  103. A phone number can be used instead of an email as a primary account confirmation
  104. mechanism.
  105. </p>
  106. <p>
  107. The user will receive a mobile OTP via sms with which they can verify that they
  108. control the phone number.
  109. </p>
  110. <p>
  111. You must enter your own twilio credentials on the auth settings page to enable sms
  112. confirmations.
  113. </p>
  114. </>
  115. }
  116. snippets={
  117. <CodeSnippet
  118. selectedLang={selectedLang}
  119. snippet={Snippets.authPhoneSignUp(endpoint, keyToShow)}
  120. />
  121. }
  122. />
  123. <DocSection
  124. title="Login via SMS OTP"
  125. content={
  126. <>
  127. <p>
  128. SMS OTPs work like magic links, except you have to provide an interface for the user
  129. to verify the 6 digit number they receive.
  130. </p>
  131. <p>
  132. You must enter your own twilio credentials on the auth settings page to enable
  133. SMS-based Logins.
  134. </p>
  135. </>
  136. }
  137. snippets={
  138. <CodeSnippet
  139. selectedLang={selectedLang}
  140. snippet={Snippets.authMobileOTPLogin(endpoint, keyToShow)}
  141. />
  142. }
  143. />
  144. <DocSection
  145. title="Verify an SMS OTP"
  146. content={
  147. <>
  148. <p>
  149. Once the user has received the OTP, have them enter it in a form and send it for
  150. verification
  151. </p>
  152. <p>
  153. You must enter your own twilio credentials on the auth settings page to enable
  154. SMS-based OTP verification.
  155. </p>
  156. </>
  157. }
  158. snippets={
  159. <CodeSnippet
  160. selectedLang={selectedLang}
  161. snippet={Snippets.authMobileOTPVerify(endpoint, keyToShow)}
  162. />
  163. }
  164. />
  165. {authenticationSignInProviders && (
  166. <DocSection
  167. title="Log in with Third Party OAuth"
  168. content={
  169. <>
  170. <p>
  171. Users can log in with Third Party OAuth like Google, Facebook, GitHub, and more. You
  172. must first enable each of these in the Auth Providers settings{' '}
  173. <span className="text-green-500">
  174. <InlineLink key={'AUTH'} href={`/project/${projectRef}/auth/providers`}>
  175. here
  176. </InlineLink>
  177. </span>{' '}
  178. .
  179. </p>
  180. <p>
  181. View all the available{' '}
  182. <InlineLink href={`${DOCS_URL}/guides/auth#providers`}>
  183. Third Party OAuth providers
  184. </InlineLink>
  185. </p>
  186. <p>
  187. After they have logged in, all interactions using the Briven JS client will be
  188. performed as "that user".
  189. </p>
  190. <p>
  191. Generate your Client ID and secret from:{` `}
  192. <InlineLink href="https://console.developers.google.com/apis/credentials">
  193. Google
  194. </InlineLink>
  195. ,{` `}
  196. <InlineLink href="https://github.com/settings/applications/new">GitHub</InlineLink>,
  197. {` `}
  198. <InlineLink href="https://gitlab.com/oauth/applications">GitLab</InlineLink>,{` `}
  199. <InlineLink href="https://developers.facebook.com/apps/">Facebook</InlineLink>,{` `}
  200. <InlineLink href="https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/">
  201. Bitbucket
  202. </InlineLink>
  203. .
  204. </p>
  205. </>
  206. }
  207. snippets={
  208. <CodeSnippet
  209. selectedLang={selectedLang}
  210. snippet={Snippets.authThirdPartyLogin(endpoint, keyToShow)}
  211. />
  212. }
  213. />
  214. )}
  215. <DocSection
  216. title="User"
  217. content={<p>Get the JSON object for the logged in user.</p>}
  218. snippets={
  219. <CodeSnippet
  220. selectedLang={selectedLang}
  221. snippet={Snippets.authUser(endpoint, keyToShow)}
  222. />
  223. }
  224. />
  225. <DocSection
  226. title="Forgotten Password Email"
  227. content={
  228. <p>
  229. Sends the user a log in link via email. Once logged in you should direct the user to a
  230. new password form. And use "Update User" below to save the new password.
  231. </p>
  232. }
  233. snippets={
  234. <CodeSnippet
  235. selectedLang={selectedLang}
  236. snippet={Snippets.authRecover(endpoint, keyToShow)}
  237. />
  238. }
  239. />
  240. <DocSection
  241. title="Update User"
  242. content={
  243. <p>
  244. Update the user with a new email or password. Each key (email, password, and data) is
  245. optional
  246. </p>
  247. }
  248. snippets={
  249. <CodeSnippet
  250. selectedLang={selectedLang}
  251. snippet={Snippets.authUpdate(endpoint, keyToShow)}
  252. />
  253. }
  254. />
  255. <DocSection
  256. title="Log out"
  257. content={
  258. <p>
  259. After calling log out, all interactions using the Briven JS client will be
  260. "anonymous".
  261. </p>
  262. }
  263. snippets={
  264. <CodeSnippet
  265. selectedLang={selectedLang}
  266. snippet={Snippets.authLogout(endpoint, keyToShow)}
  267. />
  268. }
  269. />
  270. <DocSection
  271. title="Send a User an Invite over Email"
  272. content={
  273. <>
  274. <p>Send a user a passwordless link which they can use to sign up and log in.</p>
  275. <p>
  276. After they have clicked the link, all interactions using the Briven JS client will
  277. be performed as "that user".
  278. </p>
  279. <p>
  280. This endpoint requires you use the <code>service_role_key</code> when initializing the
  281. client, and should only be invoked from the server, never from the client.
  282. </p>
  283. </>
  284. }
  285. snippets={
  286. <CodeSnippet
  287. selectedLang={selectedLang}
  288. snippet={Snippets.authInvite(endpoint, keyToShow)}
  289. />
  290. }
  291. />
  292. </div>
  293. )
  294. }