Skip to content

Commit

Permalink
Add auth storage
Browse files Browse the repository at this point in the history
  • Loading branch information
phisn committed Apr 3, 2024
1 parent 1852373 commit 42c31f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
19 changes: 18 additions & 1 deletion packages/web/src/app/layout/auth-button/AuthButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useGoogleLogin } from "@react-oauth/google"
import { useState } from "react"
import { useEffect, useState } from "react"
import { useAppStore } from "../../../common/storage/app-store"
import { trpcNative } from "../../../common/trpc/trpc-native"
import { CreateAccount } from "./CreateAccount"
Expand All @@ -14,6 +14,23 @@ export function AuthButton() {
const updateJwt = useAppStore(x => x.updateJwt)
const updateUser = useAppStore(x => x.updateUser)

useEffect(() => {
setTimeout(() => {
const jwt = useAppStore.getState().jwt

if (jwt) {
console.log("jwt", jwt)
setLoading(true)
trpcNative.user.me.query().then(user => {
updateUser(user)
setLoading(false)
})
} else {
console.log("no jwt")
}
}, 100)
}, [updateUser])

const login = useGoogleLogin({
onSuccess: async ({ code }) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/app/layout/auth-button/RenameAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function RenameAccount(props: { open: boolean; onFinished: () => void })
</label>
{loading === false && (
<div className="btn w-44" onClick={onRename}>
Create Account
Rename
</div>
)}
{loading && (
Expand Down
6 changes: 4 additions & 2 deletions packages/web/src/common/storage/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ export const useAppStore = create<AppStore>()(
storage: createJSONStorage(() => storage),

partialize: state => ({
jwt: state.jwt ?? undefined,
jwt: state.jwt,
}),
onRehydrateStorage: () => () => {},
onRehydrateStorage: () => state => {
console.log("rehydrated", state)
},
},
),
)

0 comments on commit 42c31f8

Please sign in to comment.