<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mon compte on yannick.services</title><link>https://yannick.services/compte/</link><description>Recent content in Mon compte on yannick.services</description><generator>Hugo -- gohugo.io</generator><language>fr</language><managingEditor>contact@yannick.services (Yannick)</managingEditor><webMaster>contact@yannick.services (Yannick)</webMaster><atom:link href="https://yannick.services/compte/index.xml" rel="self" type="application/rss+xml"/><item><title>Connexion</title><link>https://yannick.services/compte/callback/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><author>contact@yannick.services (Yannick)</author><guid>https://yannick.services/compte/callback/</guid><description>&lt;script&gt;
document.addEventListener('alpine:init', () =&gt; {
 const COGNITO_DOMAIN = 'https:\/\/auth.yannick.services';
 const COGNITO_CLIENT_ID = '76sdci6g48222d6olhqrq88529';
 const COGNITO_REDIRECT_URI = 'https:\/\/yannick.services\/compte\/callback\/';
 const COGNITO_TOKEN_URL = `${COGNITO_DOMAIN}/oauth2/token`;

 Alpine.store('auth', {
 accessToken: null,
 idToken: null,
 refreshToken: null,

 init() {
 this.accessToken = localStorage.getItem('ys_access_token');
 this.idToken = localStorage.getItem('ys_id_token');
 this.refreshToken = localStorage.getItem('ys_refresh_token');
 },

 isAuthenticated() {
 if (!this.accessToken) return false;
 if (this.isTokenExpired(this.accessToken)) {
 
 
 if (this.refreshToken) return true;
 return false;
 }
 return true;
 },

 isTokenExpired(token) {
 try {
 const payload = JSON.parse(atob(token.split('.')[1]));
 return payload.exp * 1000 &lt; Date.now();
 } catch (e) { return true; }
 },

 async refreshIfNeeded() {
 if (!this.isTokenExpired(this.accessToken)) return true;
 if (!this.refreshToken) return false;
 try {
 const resp = await fetch(COGNITO_TOKEN_URL, {
 method: 'POST',
 headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
 body: new URLSearchParams({
 grant_type: 'refresh_token',
 client_id: COGNITO_CLIENT_ID,
 refresh_token: this.refreshToken,
 }),
 });
 if (!resp.ok) throw new Error('refresh_failed');
 const data = await resp.json();
 this.setTokens(data.access_token, data.id_token, this.refreshToken);
 return true;
 } catch (e) {
 this.logout();
 return false;
 }
 },

 setTokens(access, id, refresh) {
 this.accessToken = access;
 this.idToken = id;
 this.refreshToken = refresh;
 localStorage.setItem('ys_access_token', access);
 localStorage.setItem('ys_id_token', id);
 localStorage.setItem('ys_refresh_token', refresh);
 },

 logout() {
 this.accessToken = null;
 this.idToken = null;
 this.refreshToken = null;
 localStorage.removeItem('ys_access_token');
 localStorage.removeItem('ys_id_token');
 localStorage.removeItem('ys_refresh_token');
 
 var logoutUrl = COGNITO_DOMAIN + '/logout?client_id=' + COGNITO_CLIENT_ID + '&amp;logout_uri=' + encodeURIComponent('https://yannick.services/');
 window.location.href = logoutUrl;
 },

 async apiFetch(url, options) {
 if (!options) options = {};
 const ok = await this.refreshIfNeeded();
 if (!ok) return null;
 const headers = options.headers || {};
 headers['Authorization'] = 'Bearer ' + this.accessToken;
 headers['Content-Type'] = 'application/json';
 return fetch(url, {
 method: options.method || 'GET',
 headers: headers,
 body: options.body || undefined,
 });
 },

 generateCodeVerifier() {
 const array = new Uint8Array(32);
 crypto.getRandomValues(array);
 return btoa(String.fromCharCode.apply(null, array))
 .replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
 },

 async generateCodeChallenge(verifier) {
 const encoder = new TextEncoder();
 const data = encoder.encode(verifier);
 const digest = await crypto.subtle.digest('SHA-256', data);
 return btoa(String.fromCharCode.apply(null, new Uint8Array(digest)))
 .replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
 },

 async redirectToLogin() {
 const verifier = this.generateCodeVerifier();
 localStorage.setItem('ys_code_verifier', verifier);
 const challenge = await this.generateCodeChallenge(verifier);
 const params = new URLSearchParams({
 response_type: 'code',
 client_id: COGNITO_CLIENT_ID,
 redirect_uri: COGNITO_REDIRECT_URI,
 scope: 'openid email',
 code_challenge: challenge,
 code_challenge_method: 'S256',
 lang: 'fr',
 });
 window.location.href = COGNITO_DOMAIN + '/oauth2/authorize?' + params.toString();
 },
 });
});
&lt;/script&gt;


&lt;style&gt;
 .auth-callback-container {
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: center;
 min-height: 40vh;
 gap: 1rem;
 text-align: center;
 }
 .auth-callback-spinner {
 width: 2rem;
 height: 2rem;
 border: 3px solid #e5e7eb;
 border-top-color: #3b82f6;
 border-radius: 50%;
 animation: auth-cb-spin 0.6s linear infinite;
 }
 @keyframes auth-cb-spin { to { transform: rotate(360deg); } }
 .auth-callback-error {
 color: #dc2626;
 font-weight: 500;
 }
 .auth-callback-link {
 color: #3b82f6;
 text-decoration: underline;
 }
 .auth-callback-link:hover {
 color: #2563eb;
 }
&lt;/style&gt;

&lt;div class="auth-callback-container" x-data="authCallback()" x-init="handleCallback()"&gt;
 &lt;template x-if="loading"&gt;
 &lt;div&gt;
 &lt;div class="auth-callback-spinner"&gt;&lt;/div&gt;
 &lt;p class="text-sm text-neutral-500" style="margin-top: 1rem;"&gt;Connexion en cours...&lt;/p&gt;</description></item></channel></rss>