[Wallet UI] Fix: Redirect on login (#4521)

## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
The focus of this PR is to refactor the login process by simplifying the `login` function and updating the login logic in the login page component.

### Detailed summary
- Simplified `login` function in `auth.ts`
- Updated login logic in `login/page.tsx` to use the simplified `login` function and navigate to wallet page after successful login

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
This commit is contained in:
gregfromstl
2024-09-10 23:03:23 +00:00
parent 5c4b1ae6a6
commit ba3c8d0ea9
2 changed files with 7 additions and 8 deletions
@@ -2,12 +2,14 @@
import { generatePayload, getCurrentUser, login, logout } from "@/lib/auth";
import { client } from "@/lib/client";
import { useTheme } from "next-themes";
import { useRouter } from "next/navigation";
import type { VerifyLoginPayloadParams } from "thirdweb/auth";
import { ConnectEmbed } from "thirdweb/react";
import { ecosystemWallet } from "thirdweb/wallets";
export default function Page({ params }: { params: { ecosystem: string } }) {
const { theme } = useTheme();
const router = useRouter();
return (
<ConnectEmbed
@@ -18,7 +20,10 @@ export default function Page({ params }: { params: { ecosystem: string } }) {
auth={{
getLoginPayload: generatePayload,
doLogin: async (params: VerifyLoginPayloadParams) => {
await login(params, `wallet/${params.payload.address}`);
const success = await login(params);
if (success) {
router.push(`/wallet/${params.payload.address}`);
}
},
isLoggedIn: async () => !!(await getCurrentUser()),
doLogout: logout,
+1 -7
View File
@@ -28,19 +28,13 @@ const thirdwebAuth = createAuth({
export const generatePayload = async (params: GenerateLoginPayloadParams) =>
thirdwebAuth.generatePayload(params);
export async function login(
payload: VerifyLoginPayloadParams,
redirectUrl?: string,
) {
export async function login(payload: VerifyLoginPayloadParams) {
const verifiedPayload = await thirdwebAuth.verifyPayload(payload);
if (verifiedPayload.valid) {
const jwt = await thirdwebAuth.generateJWT({
payload: verifiedPayload.payload,
});
cookies().set("jwt", jwt);
if (redirectUrl) {
redirect(redirectUrl);
}
return true;
}
return false;