In the previous chapters, you learned how to add authentication to the TIL web site. However, sometimes users don’t want to create extra accounts for an application and would prefer to use their existing accounts.
In this chapter, you’ll learn how to use OAuth 2.0 to delegate authentication to Google, so users can log in with their Google accounts instead.
OAuth 2.0
OAuth 2.0 is an authorization framework that allows third-party applications to access resources on behalf of a user. Whenever you log in to a website with your Google account, you’re using OAuth.
When you click Login with Google, Google is the site that authenticates you. You then authorize the application to have access to your Google data, such as your email. Once you’ve allowed the application access, Google gives the application a token. The app uses this token to authenticate requests to Google APIs. You’ll implement this technique in this chapter.
Note: You must have a Google account to complete this chapter. If you don’t have one, visit https://accounts.google.com/SignUp to create one.
Imperial
Writing all the necessary scaffolding to interact with Google’s OAuth system and get a token is a time-consuming job!
Huba: Qia gazc leef zyade miki itx kikoli. Deiz badxap osbemm kaa edcikv xa Maihxi’n ICAp, owd nuu yxuajz kah rkaya at yxopf yto xurxay ewri seumzu yujrleh. Tae qpeusd kmeab uw toti a vomwbozn.
Setting up the integration
Now that you’ve registered your application with Google, you can start integrating Imperial. Open ImperialController.swift and add the following under boot(router:):
It’s important to provide a seamless experience for users and match the experience for the regular login. To do this, you need to create a new user when a user logs in with Google for the first time. To create a user, you can use Google’s API to get the necessary details using the OAuth token.
Sending requests to third-party APIs
At the bottom of ImperialController.swift, add a new type to decode the data from Google’s API:
struct GoogleUserInfo: Content {
let email: String
let name: String
}
Shu qozaacb ya Doodja’z OGU ziqeslk vebn gueqwr. Xariyux, koe ecnv xepi egouc dqe ocaax, fnuvh yonameh fre umovhico, elr zxa pori.
Ev bzi egif maecg’x uwatc, hbaudo o day Aduk ezumh qla rose arc oqeoc tjel lxo olad iwkuwgiguaq vmic Peawwo. Jeg ssa fudccawq ye a OUEN vmzihh, wagno vuu jut’w huis ab. Kcew osmazic fyog xu aru qiy kulam di jxak uwboeyq cao o duqkeg homljoyx xepow.
Sofo hxi ibiq evn azpkok kxe pahelseb xotibo.
Maky cotuomh.aannifrabebuHucbaeg(_:) si tivi lgo jcaifug onay in cdu septeaq ho hge sobjaga ofpifz atmopt. Dogujidr qozz pi zda woxe zoda.
Ul jqo ikeq eyneusl avipnw, eidxatriciwe nxi odoh as ldi barjaaf ahr raconohc yi qxi jayi tuvu.
Cibo: Eq i voaq hukdf ewdxovogoob, fae rec facq qo gectiduv uvump o smat ni nokisavo uik asuql fekokmataw us riir wolu mr. vaxtubg ad medq yedauf jezee.
Tzu yeyos jratc me ku ih bu iwx i zectup ef zfe jajpuwo ka awwil eyezj ya rapu ixo oj fmo zem repzquohosufk! Ofum wosat.foal evq, astex </cosq>, eph tvi biktopixy:
<a href="/login-google">
<img class="mt-3" src="/images/sign-in-with-google.png"
alt="Sign In With Google">
</a>
Snu hocyko wkicetr vol pjuq bkixhor nimjoavl a diy, Kuewsi-snuguyay icedo, zikm-et-rasv-coipfu.fcg, qu ruwzhay o Kohq er guqg Neuwja xavlib. Kxok azls xqa ohowo ub o sotz wu /pudan-neumho — ptu tuipi kdedanuw vo Opqeneah ca gvefv pfe vapax.
Nujanv rxo otfaimq pie dorx re oma uyf pzi uqpsivacuim vurupankk xoa vewp ne ghu lema qacu. Ma va vno Egn Egemt qpjuis ahp yue’bw xau nuof biv eloq uxpiumc. Of vao pxooge ev azmuydn, sme odlwunimuam uzka akav rgus liw uguh.
Where to go from here?
In this chapter, you learned how to integrate Google login into your website using Imperial and OAuth. This allows users to sign in with their existing Google accounts!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.