@@ -9,60 +9,59 @@ title: 快速开始
9
9
:::
10
10
11
11
### 准备工作
12
-
13
- 1 . 你需要在 [ lafyun.com] ( https://www .lafyun.com ) 上注册一个账户
14
- 2 . 登录到 [ lafyun.com] ( https://www .lafyun.com ) 控制台 ,点击左上角的 ` 新建 ` 按钮,创建一个空应用
15
- 3 . 待应用成功启动后,点击右侧 「开发」 按钮,进入应用的「开发控制台」,接下来,我们将在「开发控制台」 进行第一个 ` laf ` 应用的功能开发
12
+
13
+ 1 . 你需要在 [ lafyun.com] ( https://console .lafyun.com ) 上注册一个账户
14
+ 2 . 登录到 [ lafyun.com 控制台 ] ( https://console .lafyun.com ) ,点击左上角的 ` 新建 ` 按钮,创建一个空应用
15
+ 3 . 待应用成功启动后,点击右侧 「开发」 按钮,进入应用的「开发控制台」,接下来,我们将在「开发控制台」 进行第一个 ` laf ` 应用的功能开发
16
16
17
17
### 编写云函数
18
18
19
- 本教程会编写两个云函数:
19
+ 本教程会编写两个云函数:
20
+
20
21
- ` register ` 处理注册请求
21
22
- ` login ` 处理登录请求.
22
23
23
-
24
-
25
-
26
24
#### 用户注册云函数
27
-
25
+
28
26
::: info
29
27
在「云函数」管理页面,点击 「新建函数」,创建注册云函数 ` register ` ,
30
28
31
29
点击 ` register ` 函数右侧的 「开发」按钮,进入 WebIDE,编写以下代码:
32
30
:::
33
31
34
32
``` ts
35
- import cloud from ' @/cloud-sdk'
36
- import { createHash } from ' crypto'
33
+ import cloud from " @/cloud-sdk" ;
34
+ import { createHash } from " crypto" ;
37
35
38
36
exports .main = async function (ctx : FunctionContext ) {
39
- const username = ctx .body ?.username || ' '
40
- const password = ctx .body ?.password || ' '
37
+ const username = ctx .body ?.username || " " ;
38
+ const password = ctx .body ?.password || " " ;
41
39
42
40
// check param
43
- if (! / [a-zA-Z0-9 ] {3,16} / .test (username )) return { error: ' invalid username' }
44
- if (! / [a-zA-Z0-9 ] {3,16} / .test (password )) return { error: ' invalid password' }
41
+ if (! / [a-zA-Z0-9 ] {3,16} / .test (username )) return { error: " invalid username" };
42
+ if (! / [a-zA-Z0-9 ] {3,16} / .test (password )) return { error: " invalid password" };
45
43
46
44
// check username existed
47
- const db = cloud .database ()
48
- const exists = await db .collection (' users' )
45
+ const db = cloud .database ();
46
+ const exists = await db
47
+ .collection (" users" )
49
48
.where ({ username: username })
50
- .count ()
49
+ .count ();
51
50
52
- if (exists .total > 0 ) return { error: ' username already existed' }
51
+ if (exists .total > 0 ) return { error: " username already existed" };
53
52
54
53
// add user
55
- const { id } = await db .collection (' users' )
56
- .add ({
57
- username: username ,
58
- password: createHash (' sha256' ).update (password ).digest (' hex' ),
59
- created_at: new Date ()
60
- })
54
+ const { id } = await db .collection (" users" ).add ({
55
+ username: username ,
56
+ password: createHash (" sha256" ).update (password ).digest (" hex" ),
57
+ created_at: new Date (),
58
+ });
61
59
62
- console .log (' user registered: ' , id )
63
- return { data: id }
64
- }
60
+ console .log (" user registered: " , id );
61
+ return { data: id };
62
+ };
65
63
```
64
+
66
65
::: info
67
66
点击右上角的 「显示调试面板」(Ctrl/Cmd + B) 即可调试运行,点击 「保存」 & 「发布」 函数即发布上线!
68
67
:::
@@ -72,49 +71,48 @@ exports.main = async function (ctx: FunctionContext) {
72
71
> 同上,创建 ` login ` 云函数,编写以下代码:
73
72
74
73
``` ts
75
- import cloud from ' @/cloud-sdk'
76
- import { createHash } from ' crypto'
74
+ import cloud from " @/cloud-sdk" ;
75
+ import { createHash } from " crypto" ;
77
76
78
77
exports .main = async function (ctx : FunctionContext ) {
79
- const username = ctx .body ?.username || ' '
80
- const password = ctx .body ?.password || ' '
78
+ const username = ctx .body ?.username || " " ;
79
+ const password = ctx .body ?.password || " " ;
81
80
82
81
// check user login
83
- const db = cloud .database ()
84
- const res = await db .collection (' users' )
82
+ const db = cloud .database ();
83
+ const res = await db
84
+ .collection (" users" )
85
85
.where ({
86
86
username: username ,
87
- password: createHash (' sha256' ).update (password ).digest (' hex' )
87
+ password: createHash (" sha256" ).update (password ).digest (" hex" ),
88
88
})
89
- .getOne ()
89
+ .getOne ();
90
+
91
+ if (! res .data ) return { error: " invalid username or password" };
90
92
91
- if (! res .data )
92
- return { error: ' invalid username or password' }
93
-
94
93
// generate jwt token
95
- const user_id = res .data ._id
94
+ const user_id = res .data ._id ;
96
95
const payload = {
97
96
uid: user_id ,
98
- exp: Math .floor (Date .now () / 1000 ) + 60 * 60 * 24 * 7
99
- }
97
+ exp: Math .floor (Date .now () / 1000 ) + 60 * 60 * 24 * 7 ,
98
+ };
100
99
101
- const access_token = cloud .getToken (payload )
100
+ const access_token = cloud .getToken (payload );
102
101
103
102
return {
104
103
uid: res .data ._id ,
105
- access_token: access_token
106
- }
107
- }
104
+ access_token: access_token ,
105
+ };
106
+ };
108
107
```
109
108
110
109
> 点击右上角的 「显示调试面板」(Ctrl/Cmd + B) 即可调试运行,点击 「保存」 & 「发布」 函数即发布上线!
111
110
112
-
113
-
114
111
### 使用 curl 调用云函数
115
112
116
113
你可以通过云函数列表页面,查看 & 复制云函数的调用地址,
117
114
或将以下 curl 命令中的 ` APPID ` 替换成你的 APPID 后执行:
115
+
118
116
``` bash
119
117
# 注册用户
120
118
curl -X POST -H " Content-Type: application/json" -d ' {"username": "admin", "password": "admin"}' https://APPID.lafyun.com/register
@@ -133,47 +131,45 @@ curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "pass
133
131
npm install laf-client-sdk
134
132
```
135
133
136
-
137
134
``` ts
138
135
// user.ts
139
136
140
- import { Cloud } from ' laf-client-sdk'
137
+ import { Cloud } from " laf-client-sdk" ;
141
138
142
- const cloud = new Cloud ({
139
+ const cloud = new Cloud ({
143
140
baseUrl: " https://APPID.lafyun.com" ,
144
- getAccessToken : () => localStorage .getItem (' access_token' )
145
- })
141
+ getAccessToken : () => localStorage .getItem (" access_token" ),
142
+ });
146
143
147
144
// regiser function
148
145
export async function register(username : string , password : string ) {
149
- const res = await cloud .invoke (' register' , {
146
+ const res = await cloud .invoke (" register" , {
150
147
username: username ,
151
- password: password
152
- })
148
+ password: password ,
149
+ });
153
150
154
- return res
151
+ return res ;
155
152
}
156
153
157
154
// login function
158
155
export async function login(username : string , password : string ) {
159
- const res = await cloud .invoke (' login' , {
156
+ const res = await cloud .invoke (" login" , {
160
157
username: username ,
161
- password: password
162
- })
158
+ password: password ,
159
+ });
163
160
164
- if (res .access_token ) {
161
+ if (res .access_token ) {
165
162
// save token
166
- localStorage .setItem (' access_token' , res .access_token )
163
+ localStorage .setItem (" access_token" , res .access_token );
167
164
}
168
165
169
- return res
166
+ return res ;
170
167
}
171
168
```
172
169
173
170
> 最后,可以在你的 Vue/React/Angular/小程序 页面中调用这两个云函数完成具体的登录注册功能!
174
171
175
-
176
172
### 其他
177
173
178
- - 我们可以在开发控制台,查看云函数的调用日志,在线调试等
179
- - 如果调用返回 404,请检查函数名是否拼写错误,或者云函数是否已经发布
174
+ - 我们可以在开发控制台,查看云函数的调用日志,在线调试等
175
+ - 如果调用返回 404,请检查函数名是否拼写错误,或者云函数是否已经发布
0 commit comments