BrowserID: createOnClick

This commit is contained in:
Michael Snoyman 2012-12-16 09:24:12 +02:00
parent e9d7d69b22
commit d9231ff5ad
2 changed files with 31 additions and 20 deletions

View File

@ -3,6 +3,7 @@
module Yesod.Auth.BrowserId module Yesod.Auth.BrowserId
( authBrowserId ( authBrowserId
, authBrowserIdAudience , authBrowserIdAudience
, createOnClick
) where ) where
import Yesod.Auth import Yesod.Auth
@ -64,6 +65,27 @@ helper maudience = AuthPlugin
(_, []) -> badMethod (_, []) -> badMethod
_ -> notFound _ -> notFound
, apLogin = \toMaster -> do , apLogin = \toMaster -> do
onclick <- createOnClick toMaster
autologin <- fmap (== Just "true") $ lift $ lookupGetParam "autologin"
when autologin $ toWidget [julius|
#{rawJS onclick}();
|]
toWidget [hamlet|
$newline never
<p>
<a href="javascript:#{onclick}()">
<img src="https://browserid.org/i/sign_in_green.png">
|]
}
where
stripScheme t = fromMaybe t $ T.stripPrefix "//" $ snd $ T.breakOn "//" t
-- | Generates a function to handle on-click events, and returns that function
-- name.
createOnClick :: (Route Auth -> Route master) -> GWidget sub master Text
createOnClick toMaster = do
addScriptRemote browserIdJs addScriptRemote browserIdJs
onclick <- lift newIdent onclick <- lift newIdent
render <- lift getUrlRender render <- lift getUrlRender
@ -85,20 +107,9 @@ function #{rawJS onclick}() {
|] |]
autologin <- fmap (== Just "true") $ lift $ lookupGetParam "autologin" autologin <- fmap (== Just "true") $ lift $ lookupGetParam "autologin"
when autologin $ toWidget [julius| when autologin $ toWidget [julius|#{rawJS onclick}();|]
#{rawJS onclick}(); return onclick
|]
toWidget [hamlet|
$newline never
<p>
<a href="javascript:#{onclick}()">
<img src="https://browserid.org/i/sign_in_green.png">
|]
}
where where
stripScheme t = fromMaybe t $ T.stripPrefix "//" $ snd $ T.breakOn "//" t
getPath t = fromMaybe t $ do getPath t = fromMaybe t $ do
uri <- parseURI $ T.unpack t uri <- parseURI $ T.unpack t
return $ T.pack $ uriPath uri return $ T.pack $ uriPath uri

View File

@ -1,5 +1,5 @@
name: yesod-auth name: yesod-auth
version: 1.1.2.2 version: 1.1.3
license: MIT license: MIT
license-file: LICENSE license-file: LICENSE
author: Michael Snoyman, Patrick Brisbin author: Michael Snoyman, Patrick Brisbin