diff --git a/sources/tech/20180716 Building a Messenger App- Access Page.md b/sources/tech/20180716 Building a Messenger App- Access Page.md index 22c53dde6d..1e195a4b55 100644 --- a/sources/tech/20180716 Building a Messenger App- Access Page.md +++ b/sources/tech/20180716 Building a Messenger App- Access Page.md @@ -7,25 +7,23 @@ [#]: via: (https://nicolasparada.netlify.com/posts/go-messenger-access-page/) [#]: author: (Nicolás Parada https://nicolasparada.netlify.com/) -Building a Messenger App: Access Page +构建一个即时消息应用(七):Access 页面 ====== -This post is the 7th on a series: +本文是该系列的第七篇。 - * [Part 1: Schema][1] - * [Part 2: OAuth][2] - * [Part 3: Conversations][3] - * [Part 4: Messages][4] - * [Part 5: Realtime Messages][5] - * [Part 6: Development Login][6] + * [第一篇: 模式][1] + * [第二篇: OAuth][2] + * [第三篇: 对话][3] + * [第四篇: 消息][4] + * [第五篇: 实时消息][5] + * [第六篇: 仅用于开发的登录][6] +现在我们已经完成了后端,让我们转到前端。 我将采用单页应用程序方案。 +首先,我们创建一个 `static/index.html` 文件,内容如下。 -Now that we’re done with the backend, lets move to the frontend. I will go with a single-page application. - -Lets start by creating a file `static/index.html` with the following content. - -``` +```html
@@ -40,11 +38,11 @@ Lets start by creating a file `static/index.html` with the following content. ``` -This HTML file must be server for every URL and JavaScript will take care of rendering the correct page. +这个 HTML 文件必须为每个 URL 提供服务,并且将使用 JavaScript 负责呈现正确的页面。 -So lets go the the `main.go` for a moment and in the `main()` function add the following route: +因此,让我们将注意力转到 `main.go` 片刻,然后在 `main()` 函数中添加以下路由: -``` +```go router.Handle("GET", "/...", http.FileServer(SPAFileSystem{http.Dir("static")})) type SPAFileSystem struct { @@ -60,15 +58,15 @@ func (spa SPAFileSystem) Open(name string) (http.File, error) { } ``` -We use a custom file system so instead of returning `404 Not Found` for unknown URLs, it serves the `index.html`. +我们使用一个自定义的文件系统,因此它不是为未知的 URL 返回 `404 Not Found`,而是转到 `index.html`。 -### Router +### 路由器 -In the `index.html` we loaded two files: `styles.css` and `main.js`. I leave styling to your taste. +在 `index.html` 中我们加载了两个文件:`styles.css` 和 `main.js`。我把样式留给你自由发挥。 -Lets move to `main.js`. Create a `static/main.js` file with the following content: +让我们移动到 `main.js`。 创建一个包含以下内容的 `static/main.js` 文件: -``` +```javascript import { guard } from './auth.js' import Router from './router.js' @@ -98,19 +96,22 @@ function view(pageName) { } ``` -If you are follower of this blog, you already know how this works. That router is the one showed [here][7]. Just download it from [@nicolasparada/router][8] and save it to `static/router.js`. +如果您是这个博客的关注者,您已经知道它是如何工作的了。 该路由器就是在 [这里][7] 显示的那个。 只需从 [@nicolasparada/router][8] 下载并保存到 `static/router.js` 即可。 We registered four routes. At the root `/` we show the home or access page whether the user is authenticated. At `/callback` we show the callback page. On `/conversations/{conversationID}` we show the conversation or access page whether the user is authenticated and for every other URL, we show a not found page. -We tell the router to render the result to the document body and dispatch a `disconnect` event to each page before leaving. +我们注册了四条路由。 在根路由 `/` 处,我们展示 home 或 access 页面,无论用户是否通过身份验证。 在 `/callback` 中,我们展示 callback 页面。 在 `/conversations/{conversationID}` 上,我们展示对话或 access 页面,无论用户是否通过验证,对于其他 URL,我们展示一个 not found 页面。 -We have each page in a different file and we import them with the new dynamic `import()`. +我们告诉路由器将结果渲染为文档主体,并在离开之前向每个页面调度一个 `disconnect` 事件。 -### Auth +我们将每个页面放在不同的文件中,并使用新的动态 `import()` 函数导入它们。 -`guard()` is a function that given two functions, executes the first one if the user is authenticated, or the sencond one if not. It comes from `auth.js` so lets create a `static/auth.js` file with the following content: +### 身份验证 -``` +`guard()` 是一个函数,给它两个函数作为参数,如果用户通过了身份验证,则执行第一个函数,否则执行第二个。 +它来自 `auth.js`,所以我们创建一个包含以下内容的 `static/auth.js` 文件: + +```javascript export function isAuthenticated() { const token = localStorage.getItem('token') const expiresAtItem = localStorage.getItem('expires_at') @@ -150,17 +151,17 @@ export function getAuthUser() { } ``` -`isAuthenticated()` checks for `token` and `expires_at` from localStorage to tell if the user is authenticated. `getAuthUser()` gets the authenticated user from localStorage. +`isAuthenticated()` 检查 localStorage 中的 `token` 和 `expires_at`,以判断用户是否已通过身份验证。`getAuthUser()` 从 localStorage 中获取经过身份验证的用户。 -When we login, we’ll save all the data to localStorage so it will make sense. +当我们登录时,我们会将所有的数据保存到 localStorage,这样才有意义。 -### Access Page +### Access 页面 ![access page screenshot][9] -Lets start with the access page. Create a file `static/pages/access-page.js` with the following content: +让我们从 access 页面开始。 创建一个包含以下内容的文件 `static/pages/access-page.js`: -``` +```javascript const template = document.createElement('template') template.innerHTML = `