Skip to content

Commit adc054a

Browse files
committed
Redirect from index page, added endpoint field
1 parent c516bc1 commit adc054a

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Use `sample.env` as a reference.
8585

8686
Go to the Appwrite console and get the following details from the project you created.
8787

88-
- host
88+
- endpoint
8989
- project id
9090
- database id
9191
- collection id

hooks/appwrite/useAppwrite.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Models, Databases } from "appwrite";
21
import { IAccountOps, IBucketOps, ICollectionOps } from "./types";
32
import useAccount from "./useAccount";
43
import useBucket from "./useBucket";
@@ -11,9 +10,8 @@ export default function useAppwrite(): [
1110
IBucketOps
1211
] {
1312
const client = useClient(
14-
process.env.NEXT_PUBLIC_APPWRITE_HOST,
13+
process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT,
1514
process.env.NEXT_PUBLIC_APPWRITE_PROJECT,
16-
process.env.NEXT_PUBLIC_APPWRITE_SSL === "true"
1715
);
1816

1917
const account = useAccount(client);

hooks/appwrite/useClient.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { Account, Client, Databases } from "appwrite";
22
import { useMemo } from "react";
33

4+
function normalizeEndpoint(
5+
endpoint: string,
6+
): string {
7+
if (endpoint.endsWith("/")) {
8+
return endpoint.substring(0, endpoint.length - 1);
9+
}
10+
return endpoint;
11+
}
12+
413
export default function useClient(
5-
host: string,
6-
projectId: string,
7-
ssl?: boolean
14+
endpoint: string,
15+
projectId: string
816
): Client {
917
const client = useMemo(() => {
1018
return new Client()
11-
.setEndpoint(`http${ssl ? "s" : ""}://${host}/v1`)
19+
.setEndpoint(normalizeEndpoint(endpoint))
1220
.setProject(projectId);
13-
}, [host, projectId]);
21+
}, [endpoint, projectId]);
1422

1523
return client;
1624
}

pages/index.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
import Head from "next/head";
2-
import Image from "next/image";
3-
import MyHead from "../components/head";
4-
import styles from "../styles/Home.module.css";
1+
import { useRouter } from "next/router";
2+
import { useEffect } from "react";
3+
import { Text } from "@chakra-ui/react";
4+
import useAppwrite from "../hooks/appwrite/useAppwrite";
55

66
export default function Home() {
7-
return (
8-
<div className={styles.container}>
9-
<MyHead />
10-
</div>
11-
);
7+
const [account] = useAppwrite();
8+
const router = useRouter();
9+
10+
useEffect(() => {
11+
if (!account.loading) {
12+
if (account.user) {
13+
router.push("/feed");
14+
} else {
15+
router.push("/auth");
16+
}
17+
}
18+
}, [account.user, account.loading]);
19+
20+
if (account.loading) return <Text>Loading...</Text>;
21+
22+
return <></>;
1223
}

sample.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
NEXT_PUBLIC_APPWRITE_HOST=<host>
2-
NEXT_PUBLIC_APPWRITE_SSL=<true | false>
1+
NEXT_PUBLIC_APPWRITE_ENDPOINT=<host>
32
NEXT_PUBLIC_APPWRITE_PROJECT=<project id>
43
NEXT_PUBLIC_APPWRITE_DATABASE=<database id>
54
NEXT_PUBLIC_APPWRITE_COLLECTION=<collection id>

0 commit comments

Comments
 (0)