Token stuff
This commit is contained in:
		
							parent
							
								
									d3dfeeccc3
								
							
						
					
					
						commit
						d499d20a9c
					
				
					 8 changed files with 194 additions and 64 deletions
				
			
		| 
						 | 
				
			
			@ -1,11 +1,13 @@
 | 
			
		|||
import {
 | 
			
		||||
    fetchLinesIterator,
 | 
			
		||||
    maybeWithBasicAuth,
 | 
			
		||||
    maybeWithBasicAuth, maybeWithBearerAuth,
 | 
			
		||||
    topicShortUrl,
 | 
			
		||||
    topicUrl,
 | 
			
		||||
    topicUrlAuth,
 | 
			
		||||
    topicUrlJsonPoll,
 | 
			
		||||
    topicUrlJsonPollWithSince, userAuthUrl,
 | 
			
		||||
    topicUrlJsonPollWithSince,
 | 
			
		||||
    userAccountUrl,
 | 
			
		||||
    userAuthUrl,
 | 
			
		||||
    userStatsUrl
 | 
			
		||||
} from "./utils";
 | 
			
		||||
import userManager from "./UserManager";
 | 
			
		||||
| 
						 | 
				
			
			@ -144,6 +146,20 @@ class Api {
 | 
			
		|||
        console.log(`[Api] Stats`, stats);
 | 
			
		||||
        return stats;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async userAccount(baseUrl, token) {
 | 
			
		||||
        const url = userAccountUrl(baseUrl);
 | 
			
		||||
        console.log(`[Api] Fetching user account ${url}`);
 | 
			
		||||
        const response = await fetch(url, {
 | 
			
		||||
            headers: maybeWithBearerAuth({}, token)
 | 
			
		||||
        });
 | 
			
		||||
        if (response.status !== 200) {
 | 
			
		||||
            throw new Error(`Unexpected server response ${response.status}`);
 | 
			
		||||
        }
 | 
			
		||||
        const account = await response.json();
 | 
			
		||||
        console.log(`[Api] Account`, account);
 | 
			
		||||
        return account;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const api = new Api();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,6 +20,7 @@ export const topicUrlAuth = (baseUrl, topic) => `${topicUrl(baseUrl, topic)}/aut
 | 
			
		|||
export const topicShortUrl = (baseUrl, topic) => shortUrl(topicUrl(baseUrl, topic));
 | 
			
		||||
export const userStatsUrl = (baseUrl) => `${baseUrl}/user/stats`;
 | 
			
		||||
export const userAuthUrl = (baseUrl) => `${baseUrl}/user/auth`;
 | 
			
		||||
export const userAccountUrl = (baseUrl) => `${baseUrl}/user/account`;
 | 
			
		||||
export const shortUrl = (url) => url.replaceAll(/https?:\/\//g, "");
 | 
			
		||||
export const expandUrl = (url) => [`https://${url}`, `http://${url}`];
 | 
			
		||||
export const expandSecureUrl = (url) => `https://${url}`;
 | 
			
		||||
| 
						 | 
				
			
			@ -95,7 +96,6 @@ export const unmatchedTags = (tags) => {
 | 
			
		|||
    else return tags.filter(tag => !(tag in emojis));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export const maybeWithBasicAuth = (headers, user) => {
 | 
			
		||||
    if (user) {
 | 
			
		||||
        headers['Authorization'] = `Basic ${encodeBase64(`${user.username}:${user.password}`)}`;
 | 
			
		||||
| 
						 | 
				
			
			@ -103,6 +103,13 @@ export const maybeWithBasicAuth = (headers, user) => {
 | 
			
		|||
    return headers;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const maybeWithBearerAuth = (headers, token) => {
 | 
			
		||||
    if (token) {
 | 
			
		||||
        headers['Authorization'] = `Bearer ${token}`;
 | 
			
		||||
    }
 | 
			
		||||
    return headers;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const basicAuth = (username, password) => {
 | 
			
		||||
    return `Basic ${encodeBase64(`${username}:${password}`)}`;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue