cococore
cococore
DDeno
Created by cococore on 6/14/2024 in #help
deno_kv_oauth + hono how to fetch data from google profile: state, plans, method names changing.
In code examples i found in internet, used methods which already not the part of deno_kv_oauth. 1. Is there examples how to manage data for case of google(actually i plan to implement use facebook for authentication too)? 2. Will the future versions of deno_kv_oauth have changes in method names and structure of the lib? Feel free to mention me. In advance thank you for your time. The attached code in file is my attempt to get data from google. At the moment it does not work. Here is shorten version.
const oauth_config = createGoogleOAuthConfig({
redirectUri: "http://localhost:8000/callback",
scope: "https://www.googleapis.com/auth/userinfo.profile"
});

const app = new Hono()

let g_tokens:Tokens; // my weird attempt to store tokens somewhere before use inside different app.get..

app.get('/', async (c:Context) => {
const session_id = getSessionId(c.req.raw);
const is_signed_in = session_id !== undefined; //has session id cookie
// console.log({is_signed_in})

const access_token = g_tokens.accessToken;

// !!! how to get google user name or login from .profile using oauth2 deno library methods?

/* THIS IS OLD CODE PART, WITH DEPRECATED METHODS AND FOR GITHUB. was found in some examples.
const accessToken = is_signed_in
? await getSessionAccessToken(oauthClient, session_id)
: null;
const githubUser = accessToken ? await getGitHubUser(accessToken) : null;
*/

if (!is_signed_in) {
return c.html(`shortened`)
}

return c.html(`shortened`)
})

app.get("/callback", async (c:Context) => {
const { response, sessionId, tokens } = await handleCallback(c.req.raw, oauth_config);
g_tokens = {...tokens};
c.header("set-cookie", response.headers.get("set-cookie")!);
return c.redirect(response.headers.get("location")!, response.status as RedirectStatusCode);
});

Deno.serve(app.fetch)
const oauth_config = createGoogleOAuthConfig({
redirectUri: "http://localhost:8000/callback",
scope: "https://www.googleapis.com/auth/userinfo.profile"
});

const app = new Hono()

let g_tokens:Tokens; // my weird attempt to store tokens somewhere before use inside different app.get..

app.get('/', async (c:Context) => {
const session_id = getSessionId(c.req.raw);
const is_signed_in = session_id !== undefined; //has session id cookie
// console.log({is_signed_in})

const access_token = g_tokens.accessToken;

// !!! how to get google user name or login from .profile using oauth2 deno library methods?

/* THIS IS OLD CODE PART, WITH DEPRECATED METHODS AND FOR GITHUB. was found in some examples.
const accessToken = is_signed_in
? await getSessionAccessToken(oauthClient, session_id)
: null;
const githubUser = accessToken ? await getGitHubUser(accessToken) : null;
*/

if (!is_signed_in) {
return c.html(`shortened`)
}

return c.html(`shortened`)
})

app.get("/callback", async (c:Context) => {
const { response, sessionId, tokens } = await handleCallback(c.req.raw, oauth_config);
g_tokens = {...tokens};
c.header("set-cookie", response.headers.get("set-cookie")!);
return c.redirect(response.headers.get("location")!, response.status as RedirectStatusCode);
});

Deno.serve(app.fetch)
13 replies