🕑 Chapters:
- Getting Referrer URL
- Adding Forward URL to Sign In Form
- Adding Forward to Validator
- Altering Post Sign In Redirect
- Outro
📜 Transcript:
if we want a user to be logged in before
they perform some action on the site
then we would ideally want to take them
right back to that action after they're
done logging in today we're going to
learn how we can do that so if we take a
look here we have a sign in pop-up here
that allows them to enter in their
username their password to log in once
they do log in though they're taken back
to the home page which is less than
ideal so how do we alter the sign in
button to take them right back here
after they've finished signing in first
you'll want to go ahead and dive into
your application go to your sign in show
make sure that you have requests
imported from your HTTP context then off
of the request what we'll want to do is
grab the refer so that would be request
dot header and then pass in refer Adonna
still support both spellings for refer
so you could do referrer as it's
described within the spec or you could
do refer as a subscribed within English
and then you'll just want to provide
that value into the page somehow so we
can do that directly by sharing it to
the page data just like so give that a
save now let's jump into our sign and
show page which is right here and let's
perform some action to check and see
whether or not we have that value so we
could do at if refer to check and see
whether or not refers on the state and
our if if it is then we can just provide
it in as a hidden value so we can do
input type equals hidden name of forward
so that we can forward them back to
whatever the value is and value of that
refer so if we give this a save jump
back into our application here and visit
sign in take a look at the HTML for this
we should see a hidden input with the
value of whatever page it was that
referred us to our sign in page as a
brief aside I am using unpoly here so
this modal is its own page you'll see
that we're actually at the sign in page
instead of this HTTP method spoofing
page but you can still see that page in
the background so do note that this is a
completely separate page that we're on
so now we have the forward address so we
want to forward them back to after
they're finished signing in and it's
going to be sent up to our action as
part of our form data so what we want to
do is add this into our validator so
let's jump up to our validators we have
a sign in validator here let's add in
forward as a schema string and let's
make this optional jump back into our
auth controller and instead of the show
method there let's dive down into our
actual sign in method and we want to
grab this property out of our validated
data so forward there and by default
this action is just forwarding back to
the home page so we can alter this by
checking to see whether or not we
actually have a forward value if we do
then we'll forward them to that forward
value otherwise we'll continue on to the
home page so here we can do
response.redirect provide to path and
then just provide in that forward value
there if there's certain pages that you
don't want them to be forwarded onto
then you would want to perform some
check there to make sure that you're not
forwarding them to one of those pages
but for the most part this should
suffice for your needs so we go ahead
and give this a save jump back into our
application visit our sign in page once
more go ahead and sign in hit enter on
that and we're taking right back to this
page and now I can perform some action
since I'm now authenticated so there's a
really quick way how you can forward a
user back to the page that they were
just on if you require authentication to
perform some action on your site if you
learned something new or you enjoyed
this lesson please consider hitting the
like button down below and subscribing
future lessons just like this one we do
Focus heavily on Adonis JS here and you
are seeing a sneak peek at the feature
evolution of our site which is coming
soon so if you stick around and learn a
bit with us here I'd really appreciate
it thanks for watching everybody and
I'll see you in the next one
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!