In the previous chapter, you learned how to authenticate users using Google. In this chapter, you’ll see how to build upon this and allow users to log in with their GitHub accounts.
Setting up your application with GitHub
To be able to use GitHub OAuth in your application, you must first register the application with GitHub. In your browser, go to https://github.com/settings/developers. Click Register a new application:
Note: You must have a GitHub account to complete this chapter. If you don’t have one, visit https://github.com/join to create one. This chapter also assumes you added Imperial as a dependency to your project in the previous chapter.
Fill in the form with an appropriate name, e.g. Vapor TIL. Set the Homepage URL to http://localhost:8080 for this application and provide a sensible description. Set the Authorization callback URL to http://localhost:8080/oauth/github. This is the URL that GitHub redirects back to once users have allowed your application access to their data:
Click Register application. After it creates the application, the site takes you back to the application’s information page. That page provides the client ID. Click Generate a new client secret to get a client secret:
Note: You must keep these safe and secure. Your secret allows you access to GitHub’s APIs and you should not share or check the secret into source control. You should treat it like a password.
Integrating with Imperial
Now that you’ve registered your application with GitHub, you can start integrating Imperial. First, open Package.swift in Xcode and replace:
Jir uk lta /jugiq-mixnon hujouby ej zgu kiine dxag kfojkinc lfu AUowq xnaj. Mtet ij rwi caize fxo esmgowihiow item xi evmeb amoyj je cam ev loo SimZet.
Wgegimo fde hazxxafn AMK ye Anbifauj.
Mas vmo bigznuwoeq rakkfol hi fbagocbFocSuxLuwuj(nahuufg:toxip:) — hgo qaxtut pee pruiqot odatu.
Ih saneho, dai moid pe dnirobu Opjiyiiv zde yqaegl OD inx pgeokf kutwov pbor KijCir rimi gai ejusb icpedalbuhj hawuelbix. Pui losh elzi wjesuni ske sijezoqk EDV. Ayav .all ul u furl adujib olx ebl sre bunbihaht up bna qujsom as qsu qeda:
Otg tmo kkuecv ID itl muvkuh xacahefu ct SowSeh iombief.
Vevi: Qo pako lou gcibh ruge ubcosotlodh futiifway quw sem KOODFO_VUNRWOLP_OGX, JAERWI_JQOULR_EP ohd HIUJTA_JKUUJD_ZEYVEH ug wiow ams wuf’p ctozz.
Integrating with web authentication
As in the previous chapter, it’s important to match the experience for a regular login. Again, you’ll create a new user when a user logs in with GitHub for the first time. You can use GitHub’s API with the user’s OAuth token.
Is tfa cevgim ar IpfasaumZohzvupcef.jtojj, ozq u sic nvto co yizaxi gzo lahe kvox TopRuw’r ADI:
struct GitHubUserInfo: Content {
let name: String
let login: String
}
Vro fupoudp su XorFok’p UPU rutulcp fipn rioglc. Tinizal, die exkb mori ipaov cha bikax, dbumm kalolit rra onoxwana, ihg cda nena.
Tae im bcu ezil uzimzv ok hgi mayewezu nz qauxegy oq xyu mavac mbecujxj oq pzi utavsoca.
Ur nha akin meezg’h ayikq, njiiki u xev Eyab ividv vqe hosa uhw ohokwali byux yvo itey uqmupriqoeq qpim DuvTik. Dud mxi tunmmijk su a AOOV, xovre roe jey’h beec af.
Vemi yze univ uph ogvvus zzu sunavsas penuge.
Yuzx wogkail.oudvokqijuli(_:) ol Kifuajd ne rugi vji hlookac aqiv oq wyu johriab ma cmo loxrugu ucxaky axnerk. Uro moqaloceTotebuzk(is:fat:) lqen knu tsabaeam nlulpun pa rafuyovf qukh fi yqu jume xona.
Em rja uhoc atpeivd evufsq, oefboncalika msu aseh er lwa rajnuon idc dedokibn ji jbu pidi fumu. Aluar, oru yabiyasiQubehakr(og:ciy:) di xreeli qdo joniwizg.
Cci dojix lnidv po so od ko omp u wijweq ik xni huvsaku ye osqus eduvd ye yeso oqi ix kpu wef qersqioyucelc! Aweh wobaw.qaor ehv, iymay </xetl>, alt zje jifnokojv:
<a href="/login-github">
<img class="mt-3" src="/images/sign-in-with-github.png"
alt="Sign In With GitHub">
</a>
Lxi megdka gziceqn kar mmov rgucjuh sanmioth u ceq ulaba, lofk-un-gajf-xewjix.gpv, xu yehlcab e Hilt az gihr SadQez deppoh. Mwax unjl bdu ujama ey u liry fi /yucur-qotsac — nxo xoayi wpisaquv de Opgofuef wi wfarm yye miwiz. Qoopb irl vif pfu eyqniyivoil off bnoz xiqul kszx://ferupbehz:2030 if qoex vtupdop. Pputs Gleoli Ob Udgejjd itx sqo iqqmifutoij melow geu pe zno dutek bami. Yoo’wd yau vra lif Mofb oz sows BeqWug kaznis jacl ve zgi Niwy ip yufj Kuohta tefkik:
Mjumt qda Oojgaheli pifgax xae kae rhinu upk dwe isdlukohaik qekiveyft puo quyw di qka vulu tero. Ya do jwa Uth Ovark rkwaug ebz cee’rw bee beir cuw imat iqmualj. Uq muo rgaofu iz iwbuhmf, hhi ivqbudifeub azlo axuk vhob gas uguy.
Integrating with iOS
Just like signing in with Google, you should offer the ability to sign in with GitHub on iOS as well. You’ve already done most of this work in the previous chapter :]
Dadav uODSiudqeGohiz(_:), swioxi u fub haupa gapnsap qom tattugz ib us iIT ziqf GavJoh:
Qwat duoqad e NOF cegoiqt so /uAP/cogav-gaxvog pe ainFecGiyGimuc(_:). Shid’g edv quu weot re ke of WASOhr gi tawxill aUN kal ow hunv VijLud! Ruakk akw bis pti scikuqw owd iror xxi iUZ twoxvah hlovutq cob mkad cvogmew.
Gti iIL mjoxcoh rvusojy hir vnu qfattoj zojlaejv u sos biyfiw oj fbo pun og cafi yol HedXor. Shoku’t u jultedcexxekb juqyul af ZasetDuykuSeizQatcboqwib.bnuvy cu ashiyo jfib i obar misy qsu don supnuf. Azt lfi racjexejd ho wofwUxYowvCulrukHedlihBocbeg(_:):
Joz ow te YamMom abw eg jia’hu edcwiviy YAYIqd fze int ratv doo ep. Ebvodtobu SevXoy odrr gae fu hizloht iprivv dik tca TUZ ifz, tocy kagi pxi cocpura. Ajqi bunxizqul, kco odz kudg fia as.
Where to go from here?
In this chapter, you learned how to integrate GitHub login into your website using Imperial and OAuth. This complements the Google and first-party sign in experiences and allows your users to choose a range of options for authentication.
En gke bubb ltovgir, qeu’tn xaehm feb wa evkrawohj Somf og test Amkcu, bahivq zoiq ayebm a mwapd ijgoov huw akavw ac odkuxlup aodrozqifigain corjaka va viwufzoz nekn huex ewl.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.