0

I am trying the log in rest API with Drupal 8.4

For that, I am using angular 5

I have all the configurations working.

This is how I get the data from the form and send it to the UserServices. The UserServices will do the post method with the correct header, the user, and the password.

onSubmit() getting the data from the form:

onSubmit(name: string, pass:string): void {
    name = name.trim();
    if (!name) { return; }    
    let user: any = {
     name: null,
     pass: null  
    };
    user.name = {value: name};
    user.pass = { "": pass};
    console.log('Json from the form ' +JSON.stringify(user));    
    this.UserService.login(user)
      .subscribe(user => {
        console.log('subscribed to login function' + JSON.stringify(this.user));
       });
}

this is the login() function, that send the post method

login (user: User): Observable<User> {
  const url = `${this.mainUrl}/user/login?_format=json`;  
  const postReturn = this.http.post(url, user, httpHaljson);      
console.log('Login return from the user service' + JSON.stringify(postReturn));  
return postReturn    
}

This is my header i have tested with aplication json and haplication hal json

const httpHaljson = {
  headers: new HttpHeaders({ 
  "X-CSRF-Token": "Qfnczb1SUnvOAsEy0A_xuGp_rkompgO2oTkCBOSEItM",
  "Authorization": "Basic Qfnczb1SUnvOAsEy0A_xuGp_rkompgO2oTkCBOSEItM", // encoded user/pass - this is admin/123qwe
  // "Content-Type": "application/hal+json"
  "Content-Type": "application/json"
  })
};

Now, this is what i get from the console: User Login: login failed: Http failure response for http://drupal.dd:8083/user/login?_format=json: 403 Forbidden

This is how I send the data from the form: Json from the form {"name":{"value":"asd"},"pass":{"":"123"}}

Login return from the user service

{"_isScalar":false,"source":{"_isScalar":false,"source":

{"_isScalar":false,"source":{"_isScalar":true,"value":

{"url":"http://drupal.dd:8083/user/login?_format=json","body":{"name":

{"value":"asd"},"pass":

{"":"123"}},"reportProgress":false,"withCredentials":false,"responseType":"

json","method":"POST","headers":{"normalizedNames":

{},"lazyUpdate":null},"params":{"updates":null,"cloneFrom":null,"encoder":

{},"map":null},"urlWithParams":"http://drupal.dd:8083/user/login?

_format=json"},"scheduler":null},"operator":{"concurrent":1}},"operator":

{}},"operator":{}}

Any idea what am i doing wrong? thanks! enter image description here

4
  • did you find any solution?
    – Lucifer
    Commented Feb 14, 2018 at 10:16
  • check the image, in the log, in the line "json from the form" I am sending in a wrong way the array with the user and the password. I recommend your do a console log with the data that you are sending and see if it's going in the way that drupal wants.
    – ValRob
    Commented Feb 15, 2018 at 11:04
  • Hey, thanks for your reply. my problem is the wrong username and password when I try with the correct username it is working fine but wrong username return 400 bad request how you handled that one?
    – Lucifer
    Commented Feb 19, 2018 at 6:00
  • look this: stackoverflow.com/questions/19671317/…
    – ValRob
    Commented Feb 20, 2018 at 7:59

1 Answer 1

0

try

const headers= new Headers({ 
  'Content-Type': 'application/json',
  'X-CSRF-Token': 'Qfnczb1SUnvOAsEy0A_xuGp_rkompgO2oTkCBOSEItM',
  'Authorization': 'Basic Qfnczb1SUnvOAsEy0A_xuGp_rkompgO2oTkCBOSEItM'
})

let options = new RequestOptions({ headers: headers });

login (user: User): Observable<User> {
  const url = `${this.mainUrl}/user/login?_format=json`;  
  return this.http.post(url, user, options ).map((res)=>{console.log(res)});      
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.