cococore
cococore•7mo ago

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)
10 Replies
Deno AI Helper
Deno AI Helper•7mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, I can offer some general advice, but please note that this advice is not based on the Deno documentation, and I advise you to double-check the accuracy of the advice given. Looking at your code, there are a few things that stand out: 1. You are attempting to store tokens in a global variable g_tokens. This approach is not recommended for production code because it can lead to race conditions and is not safe if your server is handling multiple requests concurrently. Instead, consider using a more robust session management or state management solution. 2. The getSessionId and handleCallback functions are not defined in your snippet. Make sure these functions are properly implemented and imported if they are part of a separate module. 3. The Context type should be imported from Hono if it's not already in scope. Ensure that you have the correct imports at the top of your file. 4. The Tokens type for g_tokens is not defined in your snippet. Ensure that you have defined this type or imported it if it's part of a library. 5. The Deno.serve(app.fetch) line at the end seems to be missing the options argument. It should be something like Deno.serve({ handler: app.fetch }). 6. The RedirectStatusCode type is not defined in your snippet. If it's a custom type, make sure it's properly imported or defined. Regarding your questions: 1. For examples on how to manage data for Google or Facebook authentication, you would typically look for up-to-date examples or tutorials that use the latest version of the libraries you're working with. Since I cannot provide you with specific examples, I recommend searching for recent
Deno AI Helper
Deno AI Helper•7mo ago
articles or official library documentation. 2. As for future versions of deno_kv_oauth and potential changes in method names and structure, this information would typically be found in the library's changelog, release notes, or by following the library's development on its repository. Library authors may deprecate methods and introduce breaking changes with major version updates, so it's important to keep an eye on the library's development and update your code accordingly. Remember to always refer to the latest official documentation and resources for the most accurate and up-to-date information.
cococore
cococoreOP•7mo ago
please no AI answers anymore. If i would like communicate with crapper, i can use chatgpt, perplexity, phindcom etc. And i would like avoid the answers from tool which does not understand that code shortened, so of course imports were cut.
Leokuma
Leokuma•7mo ago
Have you set up these env vars: GOOGLE_CLIENT_ID GOOGLE_CLIENT_SECRET ?
cococore
cococoreOP•7mo ago
i will set them up, using flags(deno deploy) or .env file(for dev), the question is how to get properly .profile data using deno lib, since getGithub(Google)User methods were removed from lib. // !!! how to get google user name or login from .profile using oauth2 deno library methods? so it is not a question about error or when something not works as expected. it is question about correct way to do desirable actions using deno lib, without "reinventing bicycle" @Leokuma
Leokuma
Leokuma•7mo ago
That's beyond my knowledge 😔
cococore
cococoreOP•7mo ago
i have tiny step forward. it is wip , but the process is alive. In the examples from docs, almost everything is deprecated or wrong (i use typescript)
iuioiua
iuioiua•7mo ago
handleCallback() resolves to an object that contains the OAuth access token. Use the access token to make a request to the Google REST API for the user's name. Can you please clarify what this means?
cococore
cococoreOP•7mo ago
in short words. At the moment i not fully understand the code flow in google oauth2 staff implementation, but it works partially(probably code is dirty). In same time , i would like to have good example how to implement (not only code but code design too... files folders etc) , the double login functionality using oauth2 deno + hono, for google and facebook(to have vision how to implement multilogin based on these two properly). that it has: - signin, signout, request data(user name etc) from google oauth2 api - signin, signout, request data(user name etc) from facebook oauth2 api - good file structure of the project, not the pile of code in one place so i am in process, at the moment it looks like this https://github.com/healingdrawing/play-deno-kv-oauth and i alredy see not clear place, because some providers requires "/callback" for oauth callback so i cant just use app.get("/callback_google") etc to easily separete the flow. can not find this info at the moment, probably i could be wrong
GitHub
GitHub - healingdrawing/play-deno-kv-oauth
Contribute to healingdrawing/play-deno-kv-oauth development by creating an account on GitHub.
cococore
cococoreOP•7mo ago
wth. facking facebook requirements, to just implement oauth2 login, you need pass 100500 checks and confirm business(even if you said you are a student , and named app as "test"). Naziberg at the moment at least twitter and google alive. Facebook looks not user(developer) friendly

Did you find this page helpful?