Okay.
Lets our users to register in our system.
At the moment we have one user that we've created with create Superuser Command in the terminal, but
this is not the way to do it for the users in our application.
We should allow them to register.
That means create an account in our system and then log in with that credentials.
So where how we can actually do this.
Normally we have user enabled in the Django, but we don't have anything here that we can use for registering
new users.
So what we could do is we can do it in many different ways and depending how you would like to use your
application, basically you might have methods from the URLs that will create.
So you you can gather all the username and password and whatever fields you will need to have and then
you can create that object for for him.
Like in the similar way we've done with our custom one.
Or we can just implement the view set and we'll get all the methods that are coming with this view set
and it will be available for us.
So what I can do basically is I will copy whatever we have for the movie so far and I will paste it
here.
We don't have users yet or user view set and we can create one.
So user view set will use this user.
We have that user here imported from the out models that's built in Django user and we'll create a view
set based on that user user.
So we need to also have user serializer, we need to import it, but we don't have it yet.
So I will create that in a second.
So I will import that.
So we have a new fresh new view set.
This is exactly the same as we had it here, but for a different model that was for our own model and
that's for a built model, but not it's actually change we will use the same way as we use it with our
models.
So we have our view set here.
Let's do a serializer.
Now we have user serializer imported, but we need to create it.
So here we will do a similar thing here.
So I will just duplicate it and we need to create.
User serializer that will be model user.
We need to import this user and we can import it just like that.
So Django contrib out models and then import users and I will put it in the serializer here.
So this is all gone.
So we can do ID and user name.
For now.
And this is our serializer.
That means we can actually use this serializer with an ID, a name.
Also, I have to pass a password and I will tell you why in a second.
Basically what's happening here is we want to use this serializer because that will be our default serializer
in our views here.
So we will use the serializer for all our methods.
That means it will also be used for a post method, which is creating a record in our database, but
we will use it as a register.
That means whoever wants to register in our database, it will need to use the post method for this
view set.
So if we will use the view, set this one and this serializer.
So this serializer needs to have username and password, but also we need to do extra few things that
will prevent people to think that password.
Otherwise you can just use a method get and then you will see that password for different users.
So we also need to hash that password if we will send a request to our server or send username and password,
we want to hash it and store it in a database hashed not like a normal string because password needs
to be hashed.
Okay.
And hashed means there need to be decoded.
So you can't just open the database and see people passwords this way.
Another step we need to do is we need to go to the URL, but not this one.
We need to go to the URLs for our API.
And in the same way we register a view sets here, we can register our users.
I will duplicate here, I can do users and then I can do user view set that is available here and I
also need to import it.
So we have new view set and we have that users.
So all that is done now.
So basically what we can do is if I go to Postman now, I could possibly do.
Users and slash and then I can use method get.
Uh, I think we need to run this first.
And then send it now.
So, Abe, I think I have two slashes here and then send it now.
And you can see here my object is there.
So for first, I should not be able to use that method because then I can see all the people in the
system if I will hack that API so we can actually remove it from the view set.
And then you can see here ID username and password and the password is that long string here that I
have.
This is hashed password because as you remember I put hash a password Christian but that has been hashed
and converted to this long decoded characters here.
So what we have here is already a view set that is working, but we need to do some extra steps here.
And if I will go to the serializer, we can actually include some extra thing before we will start creating
our users so we can include here.
Extract works and that will be on an object.
And we can include here password that will be another object.
And we can do write only true and we can say required.
Also true.
So what we are saying here is we are passing more information for this field password.
It will be write only.
That means we won't be able to see it.
And also it will be required if we want to register this.
So I will save it now.
And let's come back to the postman and see how it looks like.
Now.
You can see here the password is gone because this is write only.
So we can send the password but will never be able to see that.
And this is already gone.
But it will be required if we want to send that post.
So I will have a post now.
I will go to the body and let's say I will do username.
Christian, too.
And that's the only thing I will send now.
So I will try to send it.
And you can see here, password is still required because we decided we will have write only so it will
be hidden from the get, but it is required for sending.
But if I will send it now this the password will be stored as a normal field because we'll just use
send a username and password as a normal strings and that will be stored.
We need to kind of override the built in function that is in the view set that is method create and
will implement our own and then it will be hashed.
All right, so I can show you how we can do that now.
So definition create and you can see here ID is out of field all the all that.
So we have self, which is the current data and we have validated data, the validated data.
That means it's a data coming from request that are already meet all the requirements for our model
for the user.
So we have the validated data so we can have here a user, a variable, and then you can do user object,
create user.
That's a method here that we can use and that's a special method for this user.
So it's great user and then we can do Asterix Asterix, validate the data.
So we'll take the validated data that we sent to our request and we will use the create user special
method.
What we can do here is we can return that user.
So I will save it now.
And basically this create method was already included, but we overwrite it with our own version of
it.
And then we take the validate data and we use the create user, which is special function for creating
users.
And then we return that user that has been created.
So let's come back to our postman and see what will happen if I will pass a username, a password here.
So in this I can do.
Christian.
Christian up.
Christian tool.
So username and password password like that.
I'm sending that now and you can see here ID is two and the username is Christian two.
So we have created this username using the post method for this users.
So we already have an option to log in and we already have an option to to register our user.
So our API is almost ready.
We need to do some extra steps to secure our API and turn it off the things that we don't want to expose
in our API.