use super::OAuthPlugin;
use crate::plugins::endpoint_context::{EndpointContext, WithCallbacks};
use better_auth_core::{AuthPlugin, AuthResult, AuthSchema};
use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc};
pub type OAuthVerifierFuture<'a> = Pin<Box<dyn Future<Output = AuthResult<bool>> + Send + 'a>>;
type Verify<S> = dyn for<'a> Fn(&'a str, Option<&'a str>, &'a EndpointContext<'_, S>) -> OAuthVerifierFuture<'a>
+ Send
+ Sync;
pub struct OAuthCallbacks<S: AuthSchema> {
pub(crate) verifiers: HashMap<String, Arc<Verify<S>>>,
}
impl<S: AuthSchema> Default for OAuthCallbacks<S> {
fn default() -> Self {
Self {
verifiers: HashMap::new(),
}
}
}
impl<S: AuthSchema> OAuthCallbacks<S> {
pub fn verify_id_token<F>(mut self, provider: impl Into<String>, callback: F) -> Self
where
F: for<'a> Fn(
&'a str,
Option<&'a str>,
&'a EndpointContext<'_, S>,
) -> OAuthVerifierFuture<'a>
+ Send
+ Sync
+ 'static,
{
let _ = self.verifiers.insert(provider.into(), Arc::new(callback));
self
}
}
impl OAuthPlugin {
pub fn callbacks<S: AuthSchema>(self, callbacks: OAuthCallbacks<S>) -> impl AuthPlugin<S> {
WithCallbacks {
plugin: self,
callbacks: Arc::new(callbacks),
}
}
}