[00:03] (3.90s)
hello and welcome to my channel thanks
[00:06] (6.96s)
for popping in if you're new please like
[00:09] (9.12s)
And subscribe for me if you find this
[00:11] (11.34s)
content helpful hit that notification
[00:13] (13.20s)
Bell so that you don't miss out on the
[00:14] (14.64s)
next video
[00:15] (15.66s)
comment below with your thoughts or
[00:17] (17.46s)
topics you'd like me to cover and check
[00:19] (19.14s)
out my website consultingninja.tech
[00:21] (21.90s)
with that out of the way let's do some
[00:24] (24.00s)
oauth 2 with pocket base Google and
[00:26] (26.82s)
smeltkit in this example we're going to
[00:29] (29.70s)
have a sign up form
[00:31] (31.44s)
a user can fill out the form or click
[00:33] (33.66s)
the sign in with Google button if you
[00:35] (35.88s)
click that button it will take you to
[00:37] (37.32s)
the Google sign in page and if you're
[00:39] (39.72s)
already signed into your account you can
[00:41] (41.16s)
just select one otherwise you'd enter
[00:42] (42.72s)
your name and password and then from
[00:44] (44.58s)
there we'll send this information back
[00:45] (45.96s)
to svelt kit and svelt kit will send it
[00:48] (48.24s)
to pocket base where we'll create a new
[00:50] (50.82s)
account if it doesn't exist
[00:52] (52.80s)
if you want to see how to do this stick
[00:54] (54.42s)
around that's what we're going to do
[00:55] (55.62s)
right now so the very first thing that
[00:58] (58.02s)
we need to do in order to make all of
[01:00] (60.06s)
this work is we have to have Google set
[01:02] (62.28s)
up correctly
[01:03] (63.84s)
you will have to have a Google account
[01:06] (66.08s)
and just type in Google developer
[01:10] (70.26s)
in the search bar and click the Google
[01:12] (72.60s)
developer console link that comes up
[01:17] (77.88s)
inside of here you need to create a new
[01:21] (81.72s)
this is pretty straightforward there's
[01:23] (83.64s)
no detailed information here just give
[01:25] (85.38s)
it a name that you're going to remember
[01:26] (86.64s)
the location doesn't matter
[01:29] (89.64s)
once you have a project created and I'm
[01:32] (92.28s)
just going to use the one I've already
[01:33] (93.42s)
created here
[01:35] (95.94s)
it'll take you inside of the project
[01:37] (97.74s)
dashboard and this is what you'll be
[01:40] (100.38s)
presented with now we need to go into
[01:43] (103.08s)
the navigation menu and go to apis and
[01:47] (107.52s)
services and the first thing that we
[01:49] (109.68s)
need to do inside of our project is
[01:51] (111.24s)
create an oauth consent screen
[01:57] (117.30s)
now I already have one created you can
[01:59] (119.64s)
only have one consent screen per project
[02:03] (123.06s)
and so I'll just edit this and walk
[02:04] (124.92s)
through the steps
[02:07] (127.38s)
so you have to have an app name this is
[02:09] (129.90s)
what will be presented to your users you
[02:13] (133.44s)
have to have a user support email this
[02:16] (136.38s)
would be the support email that users
[02:18] (138.96s)
would send information requests to
[02:22] (142.50s)
you don't have to give a logo that part
[02:24] (144.48s)
is optional
[02:26] (146.22s)
all of the app domain stuff is is
[02:28] (148.86s)
optional and then the authorized domains
[02:31] (151.68s)
you can enter in your authorized domain
[02:34] (154.38s)
or in this example I just put localhost
[02:38] (158.30s)
leftyourdomain.com if you are using a
[02:41] (161.16s)
production app it would be whatever your
[02:42] (162.78s)
authorized domain is and then for
[02:44] (164.46s)
developer contact information I just
[02:46] (166.56s)
have the same email address as the
[02:48] (168.30s)
support email address
[02:50] (170.46s)
in the next step
[02:52] (172.56s)
here is where we need to add or remove
[02:54] (174.66s)
Scopes and Scopes are what information
[02:57] (177.48s)
your app is going to be using from your
[03:00] (180.18s)
users this will all be empty by default
[03:03] (183.00s)
so you need to click add or remove and
[03:05] (185.10s)
the basic three that you're going to
[03:06] (186.96s)
need are the very first three
[03:09] (189.36s)
so we need to be able to see the primary
[03:11] (191.52s)
Google account email address personal
[03:13] (193.56s)
information including personal
[03:15] (195.72s)
information that has been made publicly
[03:17] (197.76s)
available and then we also need to be
[03:19] (199.68s)
able to associate someone with their
[03:21] (201.96s)
personal information on Google
[03:24] (204.24s)
if you need any other Scopes there's
[03:27] (207.30s)
many of them here they're all listed you
[03:30] (210.06s)
just select which Scopes your
[03:31] (211.50s)
application needs the one thing I'll
[03:33] (213.48s)
point out here is that the more detailed
[03:35] (215.52s)
and specific and personal information
[03:37] (217.14s)
that you need the more stringent they're
[03:40] (220.32s)
going to be in their review of your
[03:42] (222.60s)
application if you're choosing just the
[03:44] (224.40s)
basic three and you're in testing you
[03:46] (226.44s)
won't need to go through the review
[03:49] (229.56s)
so after you've selected the Scopes that
[03:52] (232.02s)
you need
[03:52] (232.92s)
then we go to the next step
[03:56] (236.28s)
you do have to have test users so in
[03:59] (239.16s)
this case I just used myself
[04:01] (241.92s)
you just click add users and add an
[04:04] (244.26s)
email address in here
[04:07] (247.62s)
and then the last step is just a review
[04:10] (250.80s)
of everything that you've selected
[04:13] (253.38s)
and then you just save it
[04:16] (256.32s)
once you have your consent screen done
[04:18] (258.60s)
now we need to create some credentials
[04:22] (262.32s)
so click on the credentials tab and we
[04:26] (266.10s)
want to create credentials
[04:29] (269.28s)
and we want to create oauth client ID
[04:31] (271.92s)
credentials
[04:35] (275.76s)
so here we select web application
[04:39] (279.90s)
this name is just a name for you this is
[04:42] (282.48s)
not going to be shown to your users you
[04:44] (284.46s)
can call this whatever you're going to
[04:45] (285.72s)
remember
[04:47] (287.10s)
for your JavaScript origins in this case
[04:49] (289.98s)
you're going to want to enter a local
[04:51] (291.72s)
host 5173
[04:56] (296.22s)
so it'll be slash slash localhost
[05:04] (304.28s)
authorized redirect Uris this is where
[05:07] (307.56s)
Google is going to send a user after
[05:10] (310.02s)
they sign into their account or select
[05:12] (312.42s)
their account
[05:13] (313.62s)
and so for us we'll need to enter
[05:17] (317.10s)
we can copy this
[05:19] (319.20s)
localhost 5173 oauth
[05:23] (323.34s)
and then click create
[05:27] (327.42s)
here it gives you your client ID and
[05:29] (329.88s)
your client secret these are the two
[05:32] (332.10s)
pieces of information that we need to
[05:33] (333.66s)
enter into pocket base I'm not going to
[05:36] (336.12s)
use these because as it pointed out in
[05:38] (338.82s)
the previous screen creating a new
[05:41] (341.10s)
client ID can take anywhere from five
[05:43] (343.86s)
minutes to several hours and so for the
[05:46] (346.74s)
sake of this video I want to make sure
[05:48] (348.60s)
that things are going to work but what
[05:50] (350.28s)
we need to do next is inside of your
[05:52] (352.86s)
pocket base dashboard
[05:55] (355.98s)
you need to go into settings
[05:59] (359.22s)
down in the authentication area select
[06:02] (362.40s)
auth providers
[06:06] (366.54s)
we need to enable Google
[06:09] (369.24s)
and then from that screen the client ID
[06:12] (372.00s)
goes in here
[06:13] (373.50s)
so you take the client ID from Google
[06:17] (377.28s)
and enter it in to the client ID section
[06:20] (380.28s)
here in Pocket base
[06:21] (381.96s)
and then we need to enter in the client
[06:24] (384.30s)
secret from this screen
[06:26] (386.34s)
into the client secret section here in
[06:29] (389.70s)
the pocket-based dashboard and then you
[06:31] (391.98s)
just click save changes so that's all
[06:34] (394.92s)
the configuration that's needed in order
[06:36] (396.96s)
to make this work with Google and with
[06:39] (399.90s)
pocket base now the rest of the magic is
[06:42] (402.12s)
going to happen in svelt kit which is
[06:43] (403.80s)
going to create our UI and run all of
[06:46] (406.14s)
the pocket-based commands so inside of
[06:48] (408.42s)
our source code our spell kit source
[06:50] (410.40s)
code the very first thing that we need
[06:52] (412.20s)
to do is let's create a
[06:54] (414.80s)
hooks.server.js file I already have one
[06:57] (417.30s)
here it's empty we'll fill it in
[06:58] (418.74s)
together and what we need to do is
[07:00] (420.30s)
inside of our hooks file we're going to
[07:02] (422.58s)
fire up a new instance of pocket base
[07:04] (424.92s)
inside of our hook
[07:06] (426.72s)
so import pocket base from pocket base
[07:13] (433.14s)
and then we're going to export
[07:15] (435.66s)
const handle
[07:18] (438.24s)
and this is going to be async of course
[07:22] (442.02s)
and we need to pull out event
[07:24] (444.60s)
and resolve this is actually going to be
[07:27] (447.06s)
a pretty basic hook here
[07:30] (450.48s)
and what we're going to do is we're
[07:31] (451.74s)
going to do
[07:33] (453.02s)
event.locals.pb is assigned new
[07:36] (456.42s)
pocket base
[07:39] (459.00s)
and then I have pocket base running
[07:40] (460.68s)
locally here at 127.0.0.1
[07:46] (466.98s)
Port 8090
[07:52] (472.26s)
and then we just need to resolve so
[07:54] (474.96s)
response equals
[07:57] (477.12s)
oh wait
[08:00] (480.92s)
resolve events
[08:05] (485.58s)
and then return the response
[08:08] (488.16s)
so now we're going to have one instance
[08:10] (490.20s)
of pocket base available in all of our
[08:12] (492.60s)
other pages so give that a save next we
[08:16] (496.08s)
need to have a sign up page so I'm not
[08:18] (498.90s)
going to go into detail into the markup
[08:20] (500.88s)
but I do want to show you inside of the
[08:23] (503.28s)
markup for the sign up page we have two
[08:26] (506.70s)
forms one is the regular sign up and
[08:29] (509.88s)
down towards the middle of this file is
[08:32] (512.46s)
a second form and what's important here
[08:34] (514.92s)
is that we're sending this form to the
[08:36] (516.72s)
oauth 2 action if they choose not to
[08:39] (519.36s)
register using the regular form and they
[08:41] (521.40s)
want to instead use Google then we're
[08:43] (523.62s)
going to Ping a separate action that is
[08:45] (525.60s)
what is important from the markup file
[08:48] (528.12s)
the rest of the magic for this section
[08:50] (530.28s)
is going to happen in our plus
[08:52] (532.22s)
page.server so inside of our plus page
[08:55] (535.14s)
dot server for our sign up page let's go
[08:58] (538.02s)
ahead and import redirect we are going
[08:59] (539.88s)
to need to be using redirect and not the
[09:02] (542.22s)
type the lowercase R redirect the actual
[09:06] (546.66s)
function
[09:07] (547.68s)
and then we want to export an empty load
[09:10] (550.68s)
function
[09:16] (556.20s)
I'm not going to make use of this for
[09:18] (558.12s)
the sake of this video but you probably
[09:20] (560.28s)
would want to in a production
[09:22] (562.20s)
application you probably would want to
[09:25] (565.02s)
use this in a production application to
[09:27] (567.78s)
get all of the authentication methods
[09:29] (569.94s)
and pass those to your page and then
[09:31] (571.80s)
only render out the ones that actually
[09:34] (574.80s)
I'm not going to get into that I'm just
[09:36] (576.66s)
going to return an empty object I did
[09:39] (579.06s)
want to make this available for you guys
[09:40] (580.50s)
though so there it is if you want to do
[09:43] (583.26s)
what we do need to do is export const
[09:47] (587.64s)
actions
[09:50] (590.22s)
and sign up this is the regular sign up
[09:53] (593.10s)
uh we'll just leave this blank as well
[09:55] (595.26s)
because we're not going to actually do
[09:57] (597.42s)
anything with that in this video
[10:02] (602.16s)
so we'll leave that one empty but we
[10:03] (603.96s)
need an o auth2
[10:07] (607.38s)
action and this will be an async action
[10:09] (609.78s)
and here we are going to get into the
[10:12] (612.30s)
magic so we want to pull out cookies
[10:14] (614.72s)
URL and locals
[10:19] (619.20s)
so the very first thing that we're going
[10:20] (620.76s)
to do inside of here is let's get our
[10:23] (623.28s)
authentication methods these are the
[10:25] (625.02s)
methods that pocket base is saying are
[10:27] (627.54s)
available so auth methods
[10:33] (633.90s)
oh wait
[10:36] (636.02s)
locals.pb
[10:37] (637.86s)
dot collection
[10:44] (644.04s)
and then do a DOT list
[10:48] (648.78s)
methods
[10:50] (650.82s)
just like that
[10:53] (653.46s)
and if you want to see what comes back
[10:55] (655.38s)
let's do a console.log
[10:57] (657.84s)
and we'll just console those out
[11:01] (661.08s)
so save that
[11:03] (663.78s)
and in our application
[11:05] (665.82s)
go to the sign up page and then click
[11:08] (668.10s)
the sign in with Google button and
[11:11] (671.34s)
this is what comes back from the list
[11:14] (674.22s)
auth methods function we have the
[11:17] (677.10s)
username and password and then here we
[11:19] (679.50s)
have auth providers and this is a list
[11:21] (681.24s)
of all of the providers that pocketbase
[11:24] (684.54s)
has enabled with the name a state a
[11:28] (688.74s)
verifier a challenge what method is
[11:31] (691.86s)
going to be used and then an auth URL
[11:35] (695.16s)
so that's what comes back in that
[11:36] (696.84s)
function so what we need to do is now
[11:40] (700.56s)
that we know what methods are there we
[11:42] (702.96s)
need to double check in our action we
[11:46] (706.08s)
need to make sure that
[11:47] (707.70s)
this went through successfully so let's
[11:50] (710.22s)
just do it if not off methods so if
[11:53] (713.94s)
there are no auth providers what we'll
[11:55] (715.44s)
do is we'll return an object that says
[11:58] (718.80s)
auth providers
[12:00] (720.96s)
empty and then outside of that if next
[12:04] (724.20s)
here we get to where we're actually
[12:05] (725.40s)
going to be doing some cool stuff the
[12:07] (727.14s)
very first thing that we need to do here
[12:08] (728.52s)
is set a redirect URL that we're going
[12:11] (731.04s)
to send with this request to Google so
[12:13] (733.98s)
const redirect
[12:16] (736.38s)
URL and we're going to use the backticks
[12:20] (740.22s)
this will be dollar sign URL dot origin
[12:24] (744.48s)
and then outside of the brackets slash
[12:26] (746.70s)
oauth so this is telling Google where
[12:29] (749.46s)
we're going to redirect and Google is
[12:31] (751.44s)
going to check that when it gets it and
[12:33] (753.54s)
then we're going to do a const Google
[12:37] (757.02s)
auth provider
[12:40] (760.20s)
and this is going to be auth methods
[12:42] (762.84s)
dot auth providers
[12:48] (768.12s)
and we only have one here so we can just
[12:49] (769.98s)
pull out the first but you could also go
[12:53] (773.40s)
through them and find the name that
[12:55] (775.20s)
you're looking for but for us it's just
[12:57] (777.00s)
going to be one auth provider it's going
[12:59] (779.22s)
to be Google and so we won't do that
[13:01] (781.68s)
here but what you would want to do here
[13:03] (783.60s)
if you have many of them is just go
[13:06] (786.12s)
through and find the one that the user
[13:07] (787.98s)
chose then we need to do a full redirect
[13:10] (790.92s)
so auth
[13:14] (794.22s)
provider
[13:16] (796.38s)
redirect
[13:18] (798.42s)
this is going to be another let's use
[13:20] (800.70s)
the backticks again here
[13:23] (803.22s)
this is going to be
[13:25] (805.02s)
a couple of things put together so let's
[13:27] (807.36s)
use the back ticks again and the first
[13:29] (809.04s)
thing we need is dollar sign brackets
[13:31] (811.98s)
Google auth
[13:33] (813.44s)
provider.auth URL this is that giant
[13:35] (815.94s)
string that we saw
[13:37] (817.68s)
and then just after that dollar sign
[13:40] (820.14s)
bracket their redirect URL which is what
[13:43] (823.92s)
we just put together
[13:50] (830.52s)
now the next two things that we need to
[13:52] (832.80s)
do are when you first fire up this list
[13:55] (835.56s)
all methods function what it does is it
[13:58] (838.32s)
generates a state and a verifier for
[14:00] (840.96s)
each auth provider we need to save both
[14:03] (843.54s)
the state and in this case also the
[14:05] (845.70s)
verifier to cookies and then we'll check
[14:08] (848.64s)
the state later and we'll have to use
[14:10] (850.62s)
that verifier later because every time
[14:13] (853.98s)
you run list auth methods it generates a
[14:16] (856.56s)
new one this actually stumped me for a
[14:18] (858.54s)
little bit but I was able to figure out
[14:20] (860.16s)
what was going on so we need to save the
[14:24] (864.84s)
and this will be Google auth
[14:26] (866.34s)
provider.state
[14:28] (868.86s)
and verifier
[14:34] (874.08s)
this is going to be Google auth
[14:35] (875.82s)
provider.code
[14:37] (877.74s)
verifier
[14:40] (880.56s)
just like that
[14:42] (882.30s)
and then we're going to set both of
[14:43] (883.56s)
those in cookies
[14:45] (885.24s)
so cookies dot set
[14:51] (891.12s)
to state
[14:54] (894.06s)
and cookies
[14:56] (896.70s)
dot set
[14:58] (898.44s)
verifier
[15:01] (901.44s)
to verifier
[15:05] (905.70s)
and then the last thing that we do here
[15:07] (907.44s)
is we throw
[15:09] (909.36s)
redirect
[15:11] (911.16s)
and this is going to be a 302 and then
[15:14] (914.10s)
we send to the full auth provider
[15:17] (917.04s)
redirect which is the specific URL for
[15:22] (922.20s)
Google with the redirect URL that Google
[15:25] (925.32s)
is going to check
[15:26] (926.94s)
so let's go ahead and give that a save
[15:28] (928.68s)
now the next thing that we need to do is
[15:31] (931.08s)
we need to create a route for receiving
[15:34] (934.20s)
this we told Google and we are sending
[15:37] (937.14s)
to Google to send this back to a
[15:39] (939.90s)
separate route that's going to be the
[15:42] (942.12s)
oauth route so create a new route called
[15:45] (945.96s)
oauth and inside of there just a plus
[15:48] (948.92s)
server.js and this is going to be a git
[15:53] (953.34s)
so first we need to import the redirect
[15:58] (958.62s)
and then we're going to do an export
[16:00] (960.62s)
const git
[16:02] (962.88s)
is assigned async
[16:06] (966.60s)
and we'll pull out locals again and URL
[16:10] (970.62s)
and cookies
[16:16] (976.38s)
now here we need to pull some stuff out
[16:19] (979.26s)
of a few different places before we
[16:21] (981.66s)
start pulling out information though we
[16:23] (983.16s)
need to set up our redirect URL again
[16:26] (986.52s)
because this gets checked
[16:29] (989.22s)
redirect URL equals
[16:31] (991.92s)
the same thing as before backticks
[16:34] (994.92s)
dollar sign brackets url.origin
[16:40] (1000.80s)
slash oauth
[16:44] (1004.04s)
then we need to pull out the expected
[16:50] (1010.70s)
this is what we saved in the cookies so
[16:53] (1013.04s)
cookies Dot get
[16:58] (1018.38s)
and then here's the one that's thumped
[17:00] (1020.06s)
me for a while and this is the expected
[17:02] (1022.88s)
verifier
[17:08] (1028.76s)
and that's cookies.get we saved that to
[17:12] (1032.30s)
verifier
[17:14] (1034.58s)
now the reason why this stumped me is
[17:16] (1036.38s)
because you should be able in theory
[17:18] (1038.78s)
well we could have pulled out that
[17:20] (1040.82s)
instance of pocket base from here
[17:23] (1043.34s)
and ran the list auth methods function
[17:26] (1046.40s)
and pulled out the verifier from there
[17:28] (1048.26s)
but looking at the pocket-based
[17:30] (1050.42s)
documentation I realized that every time
[17:32] (1052.58s)
you run that function it's generating a
[17:34] (1054.50s)
new verifier and so I was getting to
[17:36] (1056.54s)
this step and the state was matching but
[17:40] (1060.26s)
authentication was failing and that was
[17:42] (1062.72s)
the reason why so I worked through the
[17:45] (1065.66s)
problems so that you guys don't have to
[17:47] (1067.96s)
make sure to save your verifier and send
[17:52] (1072.32s)
it through cookies to the receiving
[17:54] (1074.62s)
route so that you can use that one that
[17:57] (1077.48s)
was generated with that state all right
[18:00] (1080.06s)
so now we need to pull out the state
[18:02] (1082.28s)
that Google sends back so const State
[18:05] (1085.58s)
equals a weight
[18:07] (1087.62s)
URL dot search
[18:10] (1090.02s)
params.get
[18:12] (1092.66s)
so Google is going to send back a state
[18:15] (1095.06s)
in the URL params
[18:17] (1097.94s)
and then we need a code
[18:21] (1101.60s)
this is the code that Google is sending
[18:24] (1104.66s)
await URL Dot searchpraems.get
[18:33] (1113.30s)
now if you wanted to this could be I'll
[18:35] (1115.70s)
add these in here for you guys
[18:37] (1117.62s)
these might be good places to do some
[18:39] (1119.24s)
console.logs if you're having issues
[18:42] (1122.00s)
so URL dot search params so that you can
[18:45] (1125.00s)
see what's coming back from Google
[18:48] (1128.00s)
and then we'll add one here
[18:50] (1130.52s)
a console.log
[18:53] (1133.34s)
of the expected
[19:00] (1140.06s)
expected state
[19:04] (1144.44s)
and that will be expected state
[19:11] (1151.22s)
and then here after this
[19:14] (1154.04s)
after we pull out those params let's do
[19:19] (1159.74s)
console.log
[19:23] (1163.52s)
return State this is what's coming back
[19:27] (1167.30s)
from Google
[19:34] (1174.50s)
and then this is the returned code from
[19:36] (1176.78s)
Google
[19:40] (1180.32s)
that way you can see what's coming back
[19:42] (1182.96s)
if you run into any issues I want to
[19:45] (1185.12s)
make a comment here
[19:46] (1186.74s)
as a side effect
[19:48] (1188.96s)
of this next function so the next thing
[19:51] (1191.12s)
we're going to do is we're actually
[19:51] (1191.90s)
going to run to that list off methods
[19:54] (1194.54s)
again just to make sure that pocket base
[19:56] (1196.82s)
is recognizing this as a legitimate
[19:59] (1199.64s)
authentication method before we even
[20:01] (1201.20s)
attempt
[20:02] (1202.82s)
so as a side effect
[20:04] (1204.64s)
this will generate a new code verifier
[20:15] (1215.90s)
hence why we need to pass the verifier
[20:22] (1222.02s)
back in
[20:24] (1224.54s)
through a cookie
[20:28] (1228.20s)
all right and then we're going to run
[20:30] (1230.24s)
the list auth methods again so const
[20:33] (1233.24s)
auth methods equals await
[20:38] (1238.60s)
locals dot PB
[20:41] (1241.70s)
dot collection
[20:45] (1245.84s)
and we're selecting users
[20:49] (1249.44s)
dot list auth methods
[20:58] (1258.32s)
and we can just do some more error
[21:00] (1260.96s)
checking here
[21:03] (1263.48s)
not auth methods
[21:08] (1268.10s)
dot auth providers
[21:14] (1274.46s)
then we can do a console
[21:23] (1283.10s)
providers and we'll just redirect back
[21:25] (1285.92s)
to sign up
[21:27] (1287.42s)
so row
[21:29] (1289.46s)
redirect
[21:33] (1293.84s)
slash sign up
[21:37] (1297.20s)
and we probably should put a question
[21:38] (1298.58s)
mark there
[21:40] (1300.20s)
because if it doesn't come back we don't
[21:42] (1302.24s)
want to completely error out
[21:44] (1304.28s)
and then pull out the provider const
[21:46] (1306.44s)
provider
[21:49] (1309.92s)
equals auth methods Dot auth
[21:53] (1313.48s)
providers Subzero again if you were
[21:56] (1316.22s)
using a whole list you would check to
[21:58] (1318.56s)
see which one is coming back you'd
[22:00] (1320.96s)
probably put that in a cookie or a param
[22:02] (1322.70s)
and then if the provider is not there so
[22:05] (1325.40s)
if not provider then we'll do a
[22:08] (1328.46s)
console.log
[22:11] (1331.82s)
and say provider not found
[22:17] (1337.64s)
and then again we can redirect back to
[22:19] (1339.74s)
sign up so we can copy this
[22:27] (1347.84s)
now we need to check the state so if the
[22:31] (1351.68s)
expected state
[22:34] (1354.08s)
is not equal to the state that Google
[22:36] (1356.90s)
sent back well then that means somebody
[22:39] (1359.84s)
messed with the interaction and you
[22:42] (1362.60s)
should not accept it
[22:45] (1365.54s)
so we'll do a console.log and say
[22:47] (1367.82s)
returned
[22:50] (1370.10s)
state does
[22:52] (1372.50s)
not match expected
[22:58] (1378.68s)
and if you wanted to you could put the
[23:00] (1380.78s)
expected state
[23:02] (1382.70s)
and the state
[23:05] (1385.22s)
so you could see the difference
[23:07] (1387.14s)
and then again we'll just redirect back
[23:09] (1389.78s)
to sign up
[23:14] (1394.58s)
now here we're going to do the actual
[23:16] (1396.50s)
attempt to authenticate using pocket
[23:20] (1400.04s)
base so let's do a try
[23:24] (1404.84s)
we're going to do a weight
[23:27] (1407.36s)
locals.pb
[23:29] (1409.64s)
dot collection
[23:36] (1416.18s)
and we're going to do Dot auth
[23:39] (1419.12s)
with oh
[23:43] (1423.22s)
to code this is the newest way of
[23:47] (1427.16s)
authenticating using oauth with
[23:49] (1429.62s)
pocketbase they have a version that you
[23:51] (1431.84s)
could run from the client I'm not a fan
[23:54] (1434.54s)
of doing things in the client these days
[23:56] (1436.70s)
I used to do everything possible from
[23:58] (1438.80s)
the client now I'm the opposite and try
[24:01] (1441.20s)
to do everything from the back end it
[24:03] (1443.06s)
just gives you more control and a little
[24:04] (1444.56s)
bit more Security in my opinion but
[24:06] (1446.42s)
that's just me so this is the way of
[24:08] (1448.16s)
doing it from the back end so what we
[24:09] (1449.60s)
need to pass this is we need to pass the
[24:12] (1452.36s)
provider
[24:14] (1454.36s)
dot name
[24:17] (1457.16s)
the code that we pulled out of the
[24:19] (1459.56s)
params the expected verifier that's the
[24:22] (1462.68s)
one that we sent back
[24:24] (1464.66s)
and the redirect URL this needs to match
[24:27] (1467.42s)
what was used in the beginning and
[24:29] (1469.76s)
optionally just after this also you
[24:32] (1472.76s)
could pass an object let's go ahead and
[24:34] (1474.38s)
do that let's add a name in here
[24:41] (1481.58s)
awesome user
[24:43] (1483.74s)
and this is going to create that user if
[24:47] (1487.34s)
it doesn't exist and in the process you
[24:50] (1490.04s)
can pass in this information it'll set
[24:51] (1491.78s)
those properties on that user when you
[24:53] (1493.82s)
create it
[24:55] (1495.80s)
all right then let's catch
[24:58] (1498.98s)
errors
[25:01] (1501.38s)
and just do a console
[25:06] (1506.12s)
say error
[25:08] (1508.94s)
signing up with
[25:15] (1515.36s)
oauth2
[25:18] (1518.42s)
and then we can just print that error
[25:23] (1523.64s)
and then lastly
[25:26] (1526.70s)
outside of the try catch we're just
[25:28] (1528.50s)
going to redirect back to wherever you'd
[25:31] (1531.44s)
uh for my sake I'll just redirect back
[25:34] (1534.50s)
to the home page just to make things
[25:37] (1537.86s)
all right to give that a save
[25:40] (1540.62s)
now in our application
[25:42] (1542.96s)
we can go to sign in with Google and we
[25:46] (1546.32s)
can select our account I'm already
[25:48] (1548.00s)
signed into my Google account so I can
[25:49] (1549.80s)
select that and it's going to send us
[25:52] (1552.26s)
back to the auth route the oauth route
[25:55] (1555.92s)
all of our functionality there is going
[25:57] (1557.66s)
to run we're going to take that expected
[26:00] (1560.24s)
State and compare it we're going to pull
[26:03] (1563.18s)
out the code and then we're going to
[26:05] (1565.04s)
Ping our pocket base with it and when
[26:07] (1567.20s)
pocket base gets it and sees that that
[26:09] (1569.12s)
user doesn't exist pocket base will add
[26:12] (1572.00s)
that user into your users collection so
[26:14] (1574.64s)
if you select users from your
[26:16] (1576.62s)
collections Tab and hit the refresh
[26:19] (1579.08s)
button there you can see our user has
[26:21] (1581.96s)
been added to our user collection with
[26:24] (1584.66s)
an ID we did not provide a username so
[26:27] (1587.24s)
pocketbase gave it one there's the email
[26:29] (1589.70s)
address from Google and the name that we
[26:33] (1593.00s)
added in that optional object when we
[26:35] (1595.46s)
did the authentication so there you have
[26:37] (1597.68s)
it there is oauth 2 authentication using
[26:41] (1601.40s)
pocket base Google and felt kit I hope
[26:44] (1604.40s)
that you found this video helpful this
[26:45] (1605.90s)
was really fun if you enjoyed this
[26:47] (1607.58s)
please like And subscribe comment below
[26:49] (1609.62s)
with your thoughts and as always have a
[26:52] (1612.02s)
great day