Skip to content

Commit

Permalink
feat: add sign up button in site header
Browse files Browse the repository at this point in the history
  • Loading branch information
hqwuzhaoyi committed Oct 21, 2023
1 parent b107e45 commit db7f2ca
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions apps/server/src/users/users.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// user.entity.ts
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
// TODO: 改成

@Entity()
class User {
@PrimaryGeneratedColumn()
Expand All @@ -9,7 +9,7 @@ class User {
@Column()
username: string;

@Column()
@Column({ nullable: true })
email: string;

@Column({ nullable: true })
Expand Down
23 changes: 0 additions & 23 deletions apps/web/app/login/components/SigningButton.tsx

This file was deleted.

14 changes: 10 additions & 4 deletions apps/web/app/signup/components/user-auth-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Icons } from "@/components/icons";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { login } from "../api/auth";
import { login, signUp } from "../api/auth";

interface UserAuthFormProps extends React.HTMLAttributes<HTMLDivElement> {
type: "signIn" | "signUp";
Expand All @@ -22,9 +22,15 @@ export function UserAuthForm({ className, type, ...props }: UserAuthFormProps) {
setIsLoading(true);

try {
const data = await login(userName, password);
console.log(data);
window.location.href = "/";
if (type === "signUp") {
const data = await signUp(userName, password);
console.log(data);
window.location.href = "/";
} else {
const data = await login(userName, password);
console.log(data);
window.location.href = "/";
}
} catch (error) {
console.log(error);
}
Expand Down
21 changes: 0 additions & 21 deletions apps/web/components/login-button.tsx

This file was deleted.

23 changes: 23 additions & 0 deletions apps/web/components/sign-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client";

import { usePathname } from "next/navigation";
import { Button } from "@/components/ui/button";
import Link from "next/link";

export const SignButton = async () => {
const pathname = usePathname();

if (pathname === "/login") {
return (
<Link href="/signup">
<Button variant="outline">Sign Up</Button>
</Link>
);
} else {
return (
<Link href="/login">
<Button variant="outline">Login</Button>
</Link>
);
}
};
9 changes: 3 additions & 6 deletions apps/web/components/site-header.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use client';
import Link from "next/link";

import { siteConfig } from "@/config/site";
import { cn } from "@/lib/utils";
import { Button, buttonVariants } from "@/components/ui/button";
// import { CommandMenu } from "@/components/command-menu"
import { buttonVariants } from "@/components/ui/button";
import { Icons } from "@/components/icons";
import { MainNav } from "@/components/main-nav";
// import { MobileNav } from "@/components/mobile-nav"
import { ModeToggle } from "@/components/mode-toggle";
import { SigningButton } from "./login-button";
import { UserSession } from "./user-session";

export function SiteHeader() {
return (
Expand Down Expand Up @@ -61,7 +58,7 @@ export function SiteHeader() {
<ModeToggle />
{/* TODO: when login show sign out button and username */}
{/* <Link href="/login"> */}
<SigningButton />
<UserSession />
{/* </Link> */}
</nav>
</div>
Expand Down
2 changes: 2 additions & 0 deletions apps/web/components/user-nav.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { Avatar, AvatarFallback, AvatarImage } from "components/ui/avatar";
import { Button } from "components/ui/button";
import {
Expand Down Expand Up @@ -60,6 +61,7 @@ export function UserNav({ user }: { user: User }) {
<DropdownMenuItem
onClick={() => {
signOut({ redirect: false }).then(() => {
router.refresh()
router.push("/login");
});
}}
Expand Down
16 changes: 16 additions & 0 deletions apps/web/components/user-session.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";
import { UserNav } from "./user-nav";
import { User, getServerSession } from "next-auth";
import { SignButton } from "./sign-button";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";

export const UserSession = async () => {
const session = await getServerSession(authOptions);

console.debug("UserSession session", session);

if (session?.user) {
return <UserNav user={session?.user as User}></UserNav>;
}
return <SignButton />;
};

0 comments on commit db7f2ca

Please sign in to comment.