Skip to content

Commit 2ea417b

Browse files
author
misostack
committed
add loading screen sample
1 parent 335274f commit 2ea417b

File tree

1 file changed

+19
-51
lines changed

1 file changed

+19
-51
lines changed

content/post/khoa-hoc-nextjs-bai-03-advanced-routing.md

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ Yêu cầu:
3030
4. Các trang thuộc nhóm đăng nhập, cũng có giao diện chung, chỉ khác phần nội dung (content).
3131
5. Toàn bộ các trang trong website đều sử dụng chung phần header, footer, khác phần nội dung chính (main content)
3232

33-
Trong bài này, chúng ta sẽ giả lập rằng sau khi khách hàng đăng nhập xong, các thông tin sẽ được lưu lại trên cookie session.
33+
## Phân quyền và chuyển hướng
34+
35+
Trong bài này, chúng ta sẽ giả lập rằng sau khi khách hàng đăng nhập xong, các thông tin sẽ được lưu lại trên cookie.
3436

3537
Do đó chúng ta sẽ cài đặt logic như sau:
3638

@@ -46,66 +48,26 @@ Trong NextJS chúng ta có thể làm như sau:
4648

4749
![image](https://gist.github.com/assets/31009750/a5eaef32-cef7-4c06-8701-517abc4921be)
4850

49-
```ts
50-
// shared/helpers.ts
51-
import { COOKIE_PREFIX } from "./constant";
52-
53-
export const setCookie = (cname: string, cvalue: string, exdays: number) => {
54-
if (exdays) {
55-
const d = new Date();
56-
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
57-
let expires = "expires=" + d.toUTCString();
58-
59-
document.cookie =
60-
`${COOKIE_PREFIX}_${cname}` + "=" + cvalue + ";" + expires + ";path=/";
61-
} else {
62-
document.cookie = `${COOKIE_PREFIX}_${cname}` + "=" + cvalue + ";path=/";
63-
}
64-
};
65-
66-
export const getCookie = (cname: string) => {
67-
let name = `${COOKIE_PREFIX}_${cname}` + "=";
68-
let ca = document.cookie.split(";");
69-
for (let i = 0; i < ca.length; i++) {
70-
let c = ca[i];
71-
while (c.charAt(0) == " ") {
72-
c = c.substring(1);
73-
}
74-
if (c.indexOf(name) == 0) {
75-
return c.substring(name.length, c.length);
76-
}
77-
}
78-
return "";
79-
};
80-
81-
export const deleteCookie = (cname: string) => {
82-
let name = `${COOKIE_PREFIX}_${cname}` + "=";
83-
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
84-
};
85-
```
86-
8751
```ts
8852
// shared/auth.ts
8953

54+
import { AppCookie, AppRoute } from "@/shared/constant";
55+
import { cookies } from "next/headers";
9056
import { redirect } from "next/navigation";
91-
import { getCookie } from "./helper";
92-
import { AppRoute, CookieName } from "./constant";
9357

9458
export const runUserGuard = () => {
95-
let err = null;
59+
const cookieStore = cookies();
60+
if (!cookieStore.has(AppCookie.UserToken)) {
61+
return false;
62+
}
9663
try {
97-
const userCookie = getCookie(CookieName.UserCookie);
64+
const userCookie = cookieStore.get(AppCookie.UserToken);
9865
if (userCookie) {
99-
const user = JSON.parse(userCookie);
66+
const user = JSON.parse(userCookie.value);
10067
return true;
10168
}
102-
} catch (err) {
103-
err = err;
104-
}
105-
if (err) {
106-
alert(err);
107-
}
108-
redirect(AppRoute.Login);
69+
} catch (error) {}
70+
return redirect(AppRoute.Login);
10971
};
11072
```
11173

@@ -128,6 +90,8 @@ export default function MyAccount() {
12890
Các bạn hãy áp dụng cho các trang còn lại.
12991
...
13092

93+
## Tạo layout dùng chung
94+
13195
Nhưng nếu tôi có hơn 10 trang như vậy thì sao, còn cách nào khác không?
13296

13397
1. Nhóm các trang thuộc my account chung 1 nhóm sử dụng chung layout
@@ -265,3 +229,7 @@ export function middleware(req: NextRequest) {
265229
```
266230

267231
![image](https://gist.github.com/assets/31009750/6e56425c-4743-4c81-917a-212004e10a2a)
232+
233+
## Loading Screen
234+
235+
![image](https://gist.github.com/assets/31009750/34e5e0b7-2cd2-4c2a-8e3b-656fbe0b4271)

0 commit comments

Comments
 (0)