-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,83 @@ | ||
import { type OAuth2Namespace, fastifyOauth2 } from '@fastify/oauth2'; | ||
import { fastifyPlugin as fp } from 'fastify-plugin'; | ||
import { Config } from '@/config/config.js'; | ||
import { objectKeys } from './utils/objectKeys.js'; | ||
import { type Querystring, handleOauth } from './handler.js'; | ||
import { supportedProviders } from './supportedProviders.js'; | ||
import type { FastifyInstance, FastifyRequest } from 'fastify'; | ||
// import { type OAuth2Namespace, fastifyOauth2 } from '@fastify/oauth2'; | ||
// import { fastifyPlugin as fp } from 'fastify-plugin'; | ||
// import { Config } from '@/config/config.js'; | ||
// import { objectKeys } from './utils/objectKeys.js'; | ||
// import { type Querystring, handleOauth } from './handler.js'; | ||
// import { supportedProviders } from './supportedProviders.js'; | ||
// import type { FastifyInstance, FastifyRequest } from 'fastify'; | ||
|
||
export type NextOauthOptions = { | ||
googleId: Config['OAUTH_GOOGLE_CLIENT_ID']; | ||
googleSecret: Config['OAUTH_GOOGLE_SECRET']; | ||
facebookId?: string; | ||
facebookSecret?: string; | ||
}; | ||
// export type NextOauthOptions = { | ||
// googleId: Config['OAUTH_GOOGLE_CLIENT_ID']; | ||
// googleSecret: Config['OAUTH_GOOGLE_SECRET']; | ||
// facebookId?: string; | ||
// facebookSecret?: string; | ||
// }; | ||
|
||
async function oauth2(app: FastifyInstance, opts: NextOauthOptions) { | ||
const providers = supportedProviders(opts, fastifyOauth2); | ||
// async function oauth2(app: FastifyInstance, opts: NextOauthOptions) { | ||
// const providers = supportedProviders(opts, fastifyOauth2); | ||
|
||
for (const provider of objectKeys(providers)) { | ||
const startRedirectPath = `/login/${provider}`; | ||
await app.register(fastifyOauth2, { | ||
name: provider, | ||
credentials: { | ||
client: { | ||
id: providers[provider].credentials.client.id, | ||
secret: providers[provider].credentials.client.secret, | ||
}, | ||
auth: providers[provider].config, | ||
}, | ||
scope: providers[provider].scope, | ||
callbackUri: (req) => { | ||
return `${Config.PROTOCOL}://${req.hostname}/login/${provider}/callback`; | ||
}, | ||
}); | ||
// for (const provider of objectKeys(providers)) { | ||
// const startRedirectPath = `/login/${provider}`; | ||
// await app.register(fastifyOauth2, { | ||
// name: provider, | ||
// credentials: { | ||
// client: { | ||
// id: providers[provider].credentials.client.id, | ||
// secret: providers[provider].credentials.client.secret, | ||
// }, | ||
// auth: providers[provider].config, | ||
// }, | ||
// scope: providers[provider].scope, | ||
// callbackUri: (req) => { | ||
// return `${Config.PROTOCOL}://${req.hostname}/login/${provider}/callback`; | ||
// }, | ||
// }); | ||
|
||
app.get(`/login/google`, async function (request, reply) { | ||
try { | ||
const validatedURI = await this[provider].generateAuthorizationUri( | ||
request, | ||
reply, | ||
); | ||
request.log.warn(validatedURI, 'redirecting to'); | ||
return reply.redirect(validatedURI); | ||
} catch (error) { | ||
request.log.warn(error); | ||
} | ||
}); | ||
// app.log.warn(provider); | ||
|
||
app.get( | ||
`/login/google/callback`, | ||
async function ( | ||
request: FastifyRequest<{ Querystring: Querystring }>, | ||
reply, | ||
) { | ||
try { | ||
await handleOauth.call(this, provider, request, reply, providers); | ||
} catch (error: any) { | ||
if (error?.data?.payload) { | ||
reply.log.error({ error: error.data.payload }, 'Error in oauth2'); | ||
return error.data.payload; | ||
} | ||
// app.get(`/login/${provider}`, async function (request, reply) { | ||
// try { | ||
// const validatedURI = await this[provider].generateAuthorizationUri( | ||
// request, | ||
// reply, | ||
// ); | ||
// request.log.warn(validatedURI, 'redirecting to'); | ||
// return reply.redirect(validatedURI); | ||
// } catch (error) { | ||
// request.log.warn(error); | ||
// } | ||
// }); | ||
|
||
// Unknwon (probably db) error | ||
request.log.warn(error, 'deu merda severa'); | ||
return reply.internalServerError( | ||
'Algo de errado aconteceu no seu login, tente novamente', | ||
); | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
// app.get( | ||
// `/login/${provider}/callback`, | ||
// async function ( | ||
// request: FastifyRequest<{ Querystring: Querystring }>, | ||
// reply, | ||
// ) { | ||
// try { | ||
// await handleOauth.call(this, provider, request, reply, providers); | ||
// } catch (error: any) { | ||
// if (error?.data?.payload) { | ||
// reply.log.error({ error: error.data.payload }, 'Error in oauth2'); | ||
// return error.data.payload; | ||
// } | ||
|
||
export default fp(oauth2, { name: 'NextOauth2' }); | ||
// // Unknwon (probably db) error | ||
// request.log.warn(error, 'deu merda severa'); | ||
// return reply.internalServerError( | ||
// 'Algo de errado aconteceu no seu login, tente novamente', | ||
// ); | ||
// } | ||
// }, | ||
// ); | ||
// } | ||
// } | ||
|
||
declare module 'fastify' { | ||
export interface FastifyInstance { | ||
google: OAuth2Namespace; | ||
facebook: OAuth2Namespace; | ||
} | ||
} | ||
// export default fp(oauth2, { name: 'NextOauth2' }); | ||
|
||
// declare module 'fastify' { | ||
// export interface FastifyInstance { | ||
// google: OAuth2Namespace; | ||
// facebook: OAuth2Namespace; | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { fastifyPlugin as fp } from 'fastify-plugin'; | ||
import { fastifyOauth2, type OAuth2Namespace } from '@fastify/oauth2'; | ||
import type { FastifyInstance } from 'fastify'; | ||
import { Config } from '@/config/config.js'; | ||
|
||
async function oauth2Debug(app: FastifyInstance, opts: Record<string, string>) { | ||
await app.register(fastifyOauth2, { | ||
name: 'google', | ||
credentials: { | ||
client: { | ||
id: opts.OAUTH_GOOGLE_CLIENT_ID, | ||
secret: opts.OAUTH_GOOGLE_SECRET, | ||
}, | ||
auth: fastifyOauth2.GOOGLE_CONFIGURATION, | ||
}, | ||
scope: ['profile', 'email'], | ||
callbackUri: (req) => | ||
`${Config.PROTOCOL}://${req.hostname}/login/google/callback`, | ||
}); | ||
|
||
app.get('/login/google', async function (request, reply) { | ||
try { | ||
const oauthRedirectURI = await this.google.generateAuthorizationUri( | ||
request, | ||
reply, | ||
); | ||
request.log.warn(oauthRedirectURI, 'redirecting to'); | ||
return reply.redirect(oauthRedirectURI); | ||
} catch (error) { | ||
request.log.warn(error); | ||
} | ||
}); | ||
|
||
app.get('/login/google/callback', async function (request, reply) { | ||
try { | ||
const { token } = | ||
await this.google.getAccessTokenFromAuthorizationCodeFlow(request); | ||
return token.access_token; | ||
} catch (error) { | ||
if (error?.data?.payload) { | ||
reply.log.error({ error: error.data.payload }, 'Error in oauth2'); | ||
return error.data.payload; | ||
} | ||
|
||
reply.log.warn(error, 'error'); | ||
return reply.status(500).send({ | ||
error, | ||
msg: 'deu pau', | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
export default fp(oauth2Debug, { name: 'NextOauth2' }); | ||
|
||
declare module 'fastify' { | ||
export interface FastifyInstance { | ||
google: OAuth2Namespace; | ||
} | ||
} |