截屏2021-10-24 18.18.46.png

Flarum是一个开源的现代化论坛社区程序,该教程适合自己app账户系统打通Flarum账户体系实现自动登录,或者在app自动注册Flarum账户。

设置token

Flarum提供了API接口,但如果使用的话需要配置一些内容,首先安装flagrow/flarum-api-client,生成一个随机的40个字符的masterToken,之后在数据库中找到api_keys表,将该key保存进去。

之后在每个网络请求的header中添加Token,<masterToken>修改为上面的随机token

Authorization: Token <masterToken>; userId=1

注册Flarum用户

接口/api/users,请求方式POST,请求参数

{ "data": { "attributes": { "username": "test", "email": "test@gmail.com", "password": "test123" } } }

获取token

注册之后,获取用户的token

接口/api/tokenpost请求方式,请求参数

{ "identification": "Toby", "password": "pass7word" }

返回的内容如下

{ "token": "YACub2KLfe8mfmHPcUKtt6t2SMJOGPXnZbqhc3nX", "userId": "1" }

设置cookie

将用户token保存到网页的cookies中,参数名为flarum_remember

以iOS为例,代码如下,domain和token修改为真实的token即可

let webConfig = WKWebViewConfiguration() //写入token自动登录 if let cookie = HTTPCookie(properties: [ .domain: "example.com", .path: "/", .name: "flarum_remember", .value: token, .secure: "TRUE", .expires: NSDate(timeIntervalSinceNow: 31556926) ]) { webConfig.websiteDataStore.httpCookieStore.setCookie(cookie) } let webVC = WKWebView.init(frame: .zero, configuration: webConfig) webVC.hidesBottomBarWhenPushed = true self.navigationController?.pushViewController(webVC, animated: true)

这样就实现了app内跳转自动登录的效果

更新用户信息

接口/api/users/:id,请求方式PATCH,参数和注册一样,可以单项更新,也可以多项更新

{ "data": { "attributes": { "username": "test", "email": "test@gmail.com", "password": "test123" } } }

参考文章


☟☟可点击下方广告支持一下☟☟

最后修改:2021 年 10 月 24 日
请我喝杯可乐,请随意打赏: ☞已打赏列表