Expose Sanitized User on the frontend (#1190)

This commit is contained in:
Filip Sodić 2023-05-04 11:47:56 +02:00 committed by GitHub
parent 3f16e92b58
commit 29889a9c37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 17 deletions

View File

@ -0,0 +1,2 @@
// todo(filip): turn into a proper import/path
export { type SanitizedUser as User } from '../../../server/src/_types/'

View File

@ -3,10 +3,10 @@ import { deserialize as superjsonDeserialize } from 'superjson'
import { useQuery } from '../queries' import { useQuery } from '../queries'
import api, { handleApiError } from '../api' import api, { handleApiError } from '../api'
import { HttpMethod } from '../types' import { HttpMethod } from '../types'
// todo(filip): turn into a proper import import type { User } from './types'
import { type SanitizedUser as User } from '../../../server/src/_types/'
import { addMetadataToQuery } from '../queries/core' import { addMetadataToQuery } from '../queries/core'
export const getMe = createUserGetter() export const getMe = createUserGetter()
export default function useAuth(queryFnArgs?: unknown, config?: any) { export default function useAuth(queryFnArgs?: unknown, config?: any) {

View File

@ -102,6 +102,7 @@ waspComplexTest/.wasp/out/web-app/src/auth/helpers/user.ts
waspComplexTest/.wasp/out/web-app/src/auth/logout.js waspComplexTest/.wasp/out/web-app/src/auth/logout.js
waspComplexTest/.wasp/out/web-app/src/auth/pages/OAuthCodeExchange.jsx waspComplexTest/.wasp/out/web-app/src/auth/pages/OAuthCodeExchange.jsx
waspComplexTest/.wasp/out/web-app/src/auth/pages/createAuthRequiredPage.jsx waspComplexTest/.wasp/out/web-app/src/auth/pages/createAuthRequiredPage.jsx
waspComplexTest/.wasp/out/web-app/src/auth/types.ts
waspComplexTest/.wasp/out/web-app/src/auth/useAuth.ts waspComplexTest/.wasp/out/web-app/src/auth/useAuth.ts
waspComplexTest/.wasp/out/web-app/src/config.js waspComplexTest/.wasp/out/web-app/src/config.js
waspComplexTest/.wasp/out/web-app/src/entities/index.ts waspComplexTest/.wasp/out/web-app/src/entities/index.ts

View File

@ -706,12 +706,19 @@
], ],
"eb3eced8b273e539f1f0d06a4f99194237e414d208136ac5ac3da6e3c52c2566" "eb3eced8b273e539f1f0d06a4f99194237e414d208136ac5ac3da6e3c52c2566"
], ],
[
[
"file",
"web-app/src/auth/types.ts"
],
"b6572845796b4217c142b855fc72652d5dfd67fd317b0ed5487fa70ba3d92219"
],
[ [
[ [
"file", "file",
"web-app/src/auth/useAuth.ts" "web-app/src/auth/useAuth.ts"
], ],
"8d4051f75e47c6bfa1d44ef2ecf252e38988795c1a8b795cdf5ca3c7a1ac065d" "f730cb58a5ebd12285b7568bec34f4e5615261580cff1727e64b5c871d784d62"
], ],
[ [
[ [

View File

@ -0,0 +1,2 @@
// todo(filip): turn into a proper import/path
export { type SanitizedUser as User } from '../../../server/src/_types/'

View File

@ -2,10 +2,10 @@ import { deserialize as superjsonDeserialize } from 'superjson'
import { useQuery } from '../queries' import { useQuery } from '../queries'
import api, { handleApiError } from '../api' import api, { handleApiError } from '../api'
import { HttpMethod } from '../types' import { HttpMethod } from '../types'
// todo(filip): turn into a proper import import type { User } from './types'
import { type SanitizedUser as User } from '../../../server/src/_types/'
import { addMetadataToQuery } from '../queries/core' import { addMetadataToQuery } from '../queries/core'
export const getMe = createUserGetter() export const getMe = createUserGetter()
export default function useAuth(queryFnArgs?: unknown, config?: any) { export default function useAuth(queryFnArgs?: unknown, config?: any) {

View File

@ -1,6 +1,6 @@
import React, { useEffect } from 'react' import React, { useEffect } from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import { User } from '@wasp/entities' import { User } from '@wasp/auth/types'
import api from '@wasp/api' import api from '@wasp/api'
async function fetchCustomRoute() { async function fetchCustomRoute() {

View File

@ -26,24 +26,22 @@ import Wasp.Util ((<++>))
genAuth :: AppSpec -> Generator [FileDraft] genAuth :: AppSpec -> Generator [FileDraft]
genAuth spec = genAuth spec =
case maybeAuth of case maybeAuth of
Nothing -> return []
Just auth -> Just auth ->
sequence sequence
[ genLogout, [ copyTmplFile [relfile|auth/logout.js|],
copyTmplFile [relfile|auth/helpers/user.ts|],
copyTmplFile [relfile|auth/types.ts|],
genUseAuth auth, genUseAuth auth,
genCreateAuthRequiredPage auth, genCreateAuthRequiredPage auth
genUserHelpers
] ]
<++> genAuthForms auth <++> genAuthForms auth
<++> genLocalAuth auth <++> genLocalAuth auth
<++> genOAuthAuth auth <++> genOAuthAuth auth
<++> genEmailAuth auth <++> genEmailAuth auth
Nothing -> return []
where where
maybeAuth = AS.App.auth $ snd $ getApp spec maybeAuth = AS.App.auth $ snd $ getApp spec
copyTmplFile = return . C.mkSrcTmplFd
-- | Generates file with logout function to be used by Wasp developer.
genLogout :: Generator FileDraft
genLogout = return $ C.mkTmplFd (C.asTmplFile [relfile|src/auth/logout.js|])
-- | Generates HOC that handles auth for the given page. -- | Generates HOC that handles auth for the given page.
genCreateAuthRequiredPage :: AS.Auth.Auth -> Generator FileDraft genCreateAuthRequiredPage :: AS.Auth.Auth -> Generator FileDraft
@ -109,6 +107,3 @@ compileTmplToSamePath tmplFileInTmplSrcDir keyValuePairs =
C.mkTmplFdWithData C.mkTmplFdWithData
(asTmplFile $ [reldir|src|] </> tmplFileInTmplSrcDir) (asTmplFile $ [reldir|src|] </> tmplFileInTmplSrcDir)
(object keyValuePairs) (object keyValuePairs)
genUserHelpers :: Generator FileDraft
genUserHelpers = return $ C.mkTmplFd (C.asTmplFile [relfile|src/auth/helpers/user.ts|])