django rest framework - how to handle token authentication in Angular Dart? -
first time dealing spa. have back-end restful service returns token when user signs in. know supposed send token through headers in each request thinking in saving token in file , create service or class loads token in every component don't know if approach can't find documentation angular dart this.
i saved token first in localstorage tobe o suggested:
future login(username, password) async { string url = 'http://127.0.0.1:8000/auth/login/'; var response = await _client.post(url, body: {'username': username, 'password': password}); map mapped_response = _decoder.convert(response.body); window.localstorage.addall({"token": mapped_response["key"]}); }
but still receiving 401 responses when tried user information, function:
future check_authentification () async { string _headers_key = "authorization"; string _headers_value = "token "+window.localstorage["token"]; var response = await _client.get("http://127.0.0.1:8000/auth/user/", headers: {_headers_key: _headers_value}); user_data = _decoder.convert(response.body); response_status = response.statuscode; }
i couldn't authorized because django-rest-auth wasn't configured token authorization. solution add tokenauthentication default authentication classes in django settings.
rest_framework = { 'default_authentication_classes': ( 'rest_framework.authentication.basicauthentication', 'rest_framework.authentication.sessionauthentication', 'rest_framework.authentication.tokenauthentication', )}
Comments
Post a Comment