官术网_书友最值得收藏!

Summary

In this chapter, we added a useful and necessary feature to our chat application by asking users to authenticate themselves using OAuth2 service providers before we allow them to join the conversation. We made use of several open source packages, such as Gomniauth, which dramatically reduced the amount of multiserver complexity we would otherwise have dealt with.

We implemented a pattern when we wrapped http.Handler types to allow us to easily specify which paths require the user to be authenticated and which were available, even without an auth cookie. Our MustAuth helper function allowed us to generate the wrapper types in a fluent and simple way, without adding clutter and confusion to our code.

We saw how to use cookies and Base64-encoding to safely (although not securely) store the state of particular users in their respective browsers and to make use of that data over normal connections and through web sockets. We took more control of the data available to our templates in order to provide the name of the user to the UI and saw how to only provide certain data under specific conditions.

Since we needed to send and receive additional information over the web socket, we learned how easy it was to change the channels of native types into channels that work with types of our own, such as our message type. We also learned how to transmit JSON objects over the socket, rather than just slices of bytes. Thanks to the type safety of Go and the ability to specify types for channels, the compiler helps ensure that we do not send anything other than message objects through chan *message. Attempting to do so would result in a compiler error, alerting us to the fact right away.

From building a chat application to seeing the name of the person chatting is a great leap forward in terms of usability. But it's very formal and might not attract modern users of the Web, who are used to a much more visual experience. We are missing pictures of people chatting, and in the next chapter, we will explore different ways in which this could be done. We can allow users to better represent themselves in our application by pulling profile pictures (avatars) from the OAuth2 provider, the Gravatar web service, or the local disk after the users have uploaded them.

As an extra assignment, see whether you can make use of the time.Time field that we put into the message type to tell users when the messages were sent.

主站蜘蛛池模板: 彭州市| 信宜市| 资溪县| 卫辉市| 门头沟区| 教育| 巴林右旗| 石景山区| 南宁市| 大姚县| 乌兰浩特市| 房产| 绩溪县| 治县。| 南乐县| 垫江县| 聂拉木县| 静宁县| 读书| 格尔木市| 陇川县| 大渡口区| 崇阳县| 福清市| 怀仁县| 杨浦区| 新竹县| 当雄县| 克山县| 新昌县| 灵丘县| 博乐市| 远安县| 丰台区| 浏阳市| 曲麻莱县| 金溪县| 青海省| 宁国市| 阿合奇县| 云霄县|