LoginSignup
0
0

More than 5 years have passed since last update.

gistにユーザ認証が成功したかどうか。fetchで。

Last updated at Posted at 2018-03-07

まず、最も正しそうな方法。

https://api.github.com/authorizations
でチェック。ただし、basic認証のみ。

簡単な方法。HEADでチェック。

認証が成功するとapiの使用限界が変わる。60=>5000
あるいは status 401が認証に失敗したcodeとしてとりあえず返ってくる。
アクセス先は、gist内ならどこでもいい。
https://api.github.com/gists/public

 let chk=res.headers.get('X-RateLimit-Limit');
 //or res.status ==401 is NOT

全部

<input type="text"></input><input type="password"></input>
<button onclick="auth()">chk</button><label>ret</label>
let fn={}
fn.q=(d=>document.querySelector(d))
;
function isAuthGist(str){
  let url ='https://api.github.com/gists/public'
  ,opt ={method:"HEAD",mode:"cors",headers:{"Authorization":str}}
  ;
  return fetch(url,opt).then(res=>{
    let chk=res.headers.get('X-RateLimit-Limit');
    //or res.status ==401 is NOT
    console.log(res.status)
    return (chk && chk!=60)?true :false
  })
}
;
function auth(){
  let u=fn.q('input[type="text"]').value
  ,p=fn.q('input[type="password"]').value
  ,str ='basic '+ btoa(u+':'+p)
  ;
  isAuthGist(str)
    .then(d=>{fn.q('label').textContent=''+d;return d})
  ;
}
;

response header

/*NOT*/
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
Content-Length:93
Content-Security-Policy:default-src 'none'
Content-Type:application/json; charset=utf-8
Date:Wed, 07 Mar 2018 13:12:26 GMT
Server:GitHub.com
Status:401 Unauthorized
Strict-Transport-Security:max-age=31536000; includeSubdomains; preload
X-Content-Type-Options:nosniff
X-Frame-Options:deny
X-GitHub-Media-Type:github.v3; format=json
X-GitHub-Request-Id:F464:41E2:DE8869:104A905:5A9FE52F
X-RateLimit-Limit:60
X-RateLimit-Remaining:58
X-RateLimit-Reset:1520431935
X-Runtime-rack:0.061018
X-XSS-Protection:1; mode=block
/*authed*/
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
Cache-Control:private, max-age=60, s-maxage=60
Content-Encoding:gzip
Content-Security-Policy:default-src 'none'
Content-Type:application/json; charset=utf-8
Date:Wed, 07 Mar 2018 13:13:20 GMT
ETag:W/"ed8a0671388a83cfa6e5f705c5f2c191"
Link:; rel="next", ; rel="last"
Server:GitHub.com
Status:200 OK
Strict-Transport-Security:max-age=31536000; includeSubdomains; preload
Vary:Accept-Encoding
Vary:Accept, Authorization, Cookie, X-GitHub-OTP
X-Content-Type-Options:nosniff
X-Frame-Options:deny
X-GitHub-Media-Type:github.v3; format=json
X-GitHub-Request-Id:F464:41E2:DE94E1:104ACA4:5A9FE53A
X-RateLimit-Limit:5000
X-RateLimit-Remaining:4997
X-RateLimit-Reset:1520429607
X-Runtime-rack:0.237184
X-XSS-Protection:1; mode=block
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0