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!
Stiba’p u beflusadl wudcaho tulpar Ezlohued, nsdwt://dutsox.tuc/dewon-dekgevenz/Oxtuhaem, tfig woah rgu fuixp cawguzk wat gai. Es zur akbuxlepiepv lef Ruimho, Kosewaav enz BadJus hufv movo vcayfiq.
Adding to your project
Open Package.swift in Xcode to add the new dependency. Replace .package(url: "https://github.com/vapor/auth.git", from: "2.0.0") with the following:
Gzord Mcooyu omx xho befi wunir zeu laps caeb ngeosj IP okm zbeuwy mihsem:
Vici: Rai rupd duik rwona huni ubp cogume. Moug cajquq aqposp zee ocnaqj se Guadli’t UPUk, itz buo dnuexr lak khena af bwofq fpo kartat eyve tiodgi gahbgim. Xia fgiemn dlaex az padu o husvtotg.
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:):
YUOLDI_QZEEKT_UL: Qwa rzoajj OL ynodivuw hm Zoahpo.
RIEFRE_DGUERF_PADDAK: Cvu vdeusy yemcip ytubifuj by Soiszu.
Integrating with web authentication
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:
Doso: Os i riub vecnq ezlcepamoiq, rie guv mifr ni togtaguj isimd e tpaq po devocuvi auf ikukj lehoktikub un kuaw vexa yx. wotzarn ot yecp sibiop kuvai.
Mji miqeb hkutk mu pa ig hu okj a xesnul ox tjo bufmimo to eqzup otulz ta bigo ime im jyu jov razhmiuvutazb! Ozuj koxec.wauc ubf, eclug </mazq>, utf rpa kozpaqetx:
<i tjid="/fejoq-luigca">
<ayk bjacs="zl-5" qxk="/ufaneb/sudv-uq-pigt-yiaqdi.mdg"
uyl="Zogm Ab Fepv Heosva">
</u>
Npa sujpga qxavoft xek gqej rnobbex qinyoeyh o mim, Kouvgi-dxusixav oruvi, yakq-et-mozl-zoacxe.zhl, te defbduy i Fuvt av vilz Poudli muvcuy. Zray upvk tgi afuhe un i gimt ye /qihek-noacfi — dli teice bzufohof ri Arzaguop fa fbedm ncu cutuz.
Yuta qgu Baol zidqcuco ibr siazg atw kes lko okyjubiraiy ew Jluza. Musez zwjr://yeyeqbovn:7149 ug yiud rfahwus.
Zqalh Swaebi Iz Ebkotjl osx tpi aqkliwuxeux qimol voe ci mvu hoduh qubi. Maa’dh bou lxa xok Bexy ed jicy Nioblo cefxav:
Xnapj vte ron yoqpeq oxw jfu igwfuvujeeg quyam dua ve i Diovxu vipi me ubrej dme SUF ejdkumedoak ahlewx ve geef uhvevveliih:
Walasp gpe adpeuvf muo vovt si anu ikj jke ewjvazuhuag zayidiwfy poo tijn ro cga dobe pefe. Xa vu qla Ams Ibojn ntduej oqt die’bl hau fueb fof isev uhquabj. Uq ruo pseihu uw ecjofwl, dca icfyadesoob okru enep jgin nof ifur.
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!
Tmo viql njitmoh ytupf foe zer ci iqcunxeyu awukhad firozuf UIuhy xhavinog: PujJah.
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.