N
N
NewSantaClaus2022-03-23 13:09:10
Google
NewSantaClaus, 2022-03-23 13:09:10

How to change encoding before/after receiving data from Google Sign In Identity?

Help mi pliz

I do authorization using Google Sign In Identity

Authorization passes, I receive user data. But here's the sadness. When a Yurez has a name and a surname in English, then it's ok, but when in Russian I get crocozyabrs. As it turned out, he gives data in windows-1258 encoding, and I use utf-8 on the page. How to fix it? Code attached

(function(){

  window.onGoogleLibraryLoad = function () {
    google.accounts.id.initialize({
      client_id: GOOGLE_APP_ID,
      callback: function(response){
        const payload = parseJwt(response.credential)
        saveUserData({
          first_name: payload.family_name,
          last_name: payload.given_name,
          email: payload.email
        })
      }
    })

    google.accounts.id.renderButton(
      document.querySelector(GOOGLE_BTN_AUTH),
      {
        theme: "outline",
        size: "large"
      }
    )

    google.accounts.id.prompt();
  }
})();


As a result, when logging in from different accounts, I get:
// ru
const user = {first_name: 'ФамилиÑ\x8F', last_name: 'Ð\x98мÑ\x8F', email: '[email protected]'}

// en 
const user = {first_name: 'New', last_name: 'SantaClaus', email: '[email protected]'}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elvis, 2022-03-25
@Dr_Elvis

I had something similar, I just recoded the response from the server, try it, maybe you can rework it and apply it yourself

function transformWindows1251ToUTF8(response) {
  const transformedBody = response.body
    .pipeThrough(new TextDecoderStream("windows-1251"))
    .pipeThrough(new TextEncoderStream("utf-8"));
  return new Response(transformedBody);
}

fetch(`http://example.com`)
.then(transformWindows1251ToUTF8)
.then(response => response.text())
.then(function(data) {
  console.log(data);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question