Web3 User Authentication
Penny Li
1 follower
4 Video Views·Feb 21, 2024
Remix: https://remix.ethereum.org/
User Authentication Code: https://gist.github.com/LuckyPen3584/adbf9d4316476265afa1f8938508e13d
Tutorial in Text: https://luckypen.substack.com/p/ethereum-user-authentication
hi everyone and welcome to this YouTube
channel my name is Penny and this is
Penny's coding bits this channel is
dedicated to bring you weekly bsize
videos with practical solutions to your
everyday data analytical challenges and
this video is going to walk you through
how to authenticate a user in the web 3
world so here I have a very simple
solidity code that registers a user
locks in and retrieves their information
from the system so if I hit
run the code will be compiled and once I
hit
deploy it will show up in the deploy
contract
section so if I want to register myself
I'll just put my name and hit
register
and my name will show up in the decoded
input field because I am inputting
something into the system so once I'm
registered I can hit log
in and you'll see that in the log
messages for this activity it says user
logged in so I'm logged in and now I
want to retri retrieve my information
from the system and I hit get
username and you'll see that my name is
now in the decoded output field because
I'm retrieving something from the
system so now let's have a walk through
this code step by step so first of all
we need to make sure that we have the
correct version of solidity so that we
could correctly run our code so we need
pragma solidity 080 here and next we
need to Define our contract and the name
of our contract is called basic off so
we'll just put contract basic off and in
Brackets we can name all of the
functions that we need to include in
this
contract
and first and foremost we need the
users's information to be stored in the
system and that contains the user's
username and we also need to have a
Boolean variable that indicates whether
a user exists in the system or
not and these will play critical roles
in the upcoming function
functionalities and next we have a
mapping
algorithm that Maps the users ethereum
address to their user data and this is
critical in this decentralized
environment versus in our traditional
system when you log in you need your
username and password it doesn't work
like that in this environment all you
need is the ethereum address for the
user so this is very critical to
authenticate users in the web 3 uh
environment so here we are essentially
mapping the users ethereum
addresses addresses to their user
data and these are their private user
data that we're using
using and in order to register a user a
event has to be emitted upon a
successful registration and that
includes the users ethereum address and
their
username and once they are successfully
logged into the system their user
ethereum address has to be
emitted and here we have the function to
register a user so first of all it
checks whether the user exists in the
system and if it it's not existing in
the system then we go ahead and register
them with their
username and emit that the user is
registered like you have seen in the log
messages uh for my registration
earlier
and after we have the user registered we
need them to be logged in and this is
external so that uh they can access it
from outside this
contract
and as before we need to check if they
exist in the system because if they
don't already exist in the system they
can't log in so if they are existing in
the system then we go ahead and log them
in and last but not least the function
to get the users information once they
are registered in the system is
requiring us to uh check if they exist
one more time and if they do exist then
return their username according to their
ethereum address so this is in a
nutshell how to authenticate a user in
the web three environment I hope this
will shed some light on this mystery of
how to authenticate users in a
decentralized world and I'll see you in
the next video
