Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LoginUserAPIView(APIView):
- permission_classes = () # login should be accessed by anyone, no restrictions.
- def post(self, request, format=None):
- username = request.data['username']
- password = request.data['password']
- user = authenticate(username=username, password=password)
- if user is not None:
- login(request, user)
- user = UserSerializer(user)
- return Response({'success': 'Logged in', 'user': user.data})
- return Response({'wrong': 'username or password not correct, try again'}, status=status.HTTP_401_UNAUTHORIZED)
- login() {
- this.isLoading = true;
- return axios({
- method: 'post',
- url: 'http://localhost:8000/api/login_user',
- data: {
- username: this.name,
- password: this.password
- },
- headers: {
- 'Content-Type': 'application/json'
- }
- })
- .then(response => {
- this.$store.commit('loginAuth', true); // setting isAuth to true in vuex store.
- this.$store.commit('setUser', response.data.user); // to access the user object since I don't use django templates
- this.$router.push({name: 'page'}); // redirect to random test page that requires authorization so I can see if everything went fine.
- })
- .catch(err => {
- console.error(err);
- if (err.response.status == 401) {
- alert(err.response.data.wrong);
- this.isLoading = false;
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement