/
Eff.purs
91 lines (73 loc) · 4.11 KB
/
Eff.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module Web.Firebase.Authentication.Eff (
authWithCustomToken
, authWithOAuthRedirect
, authWithOAuthRedirectSilent
, onAuth
, offAuth
, unAuth
) where
import Prelude (Unit(), pure, unit)
import Web.Firebase.Types (Firebase(), FirebaseEff(), FirebaseErr)
import Web.Firebase.Authentication.Types (Auth)
import Control.Monad.Eff (Eff())
import Data.Foreign (Foreign())
import Data.Function.Uncurried (Fn4, Fn3, Fn2, Fn1, runFn4, runFn3, runFn2, runFn1)
foreign import _onAuth :: forall eff. Fn2 (Foreign -> Eff ( firebase :: FirebaseEff | eff) Unit) Firebase (Eff (firebase :: FirebaseEff | eff) Unit)
onAuth :: forall eff. (Foreign -> Eff ( firebase :: FirebaseEff | eff) Unit) -> Firebase -> Eff (firebase :: FirebaseEff | eff) Unit
onAuth = runFn2 _onAuth
-- | de-register an onAuth callback
--https://www.firebase.com/docs/web/api/firebase/offauth.html
-- for sign out functionality, see unAuth
foreign import _offAuth :: forall eff. Fn2 Firebase (Foreign -> Eff ( firebase :: FirebaseEff | eff) Unit) (Eff (firebase :: FirebaseEff | eff) Unit)
offAuth :: forall eff. Firebase -> (Foreign -> Eff ( firebase :: FirebaseEff | eff) Unit) -> Eff (firebase :: FirebaseEff | eff) Unit
offAuth = runFn2 _offAuth
-- we should also have a runFn1 for offSimple (or maybe offBasic, or offRef since we only care about tthe ref and nothing else)
-- | oAuth identification, redirecting to a provider (e.g. "twitter").
-- No idea what can be in the Error, and since it is a redirect, how is the callback going to happen?
-- perhaps only when the provider is not configured correctly in firebase?
foreign import _authWithOAuthRedirect :: forall eff. Fn3
String
(Foreign -> Eff ( firebase :: FirebaseEff | eff) Unit)
Firebase
(Eff (firebase :: FirebaseEff | eff) Unit)
type AuthenticationProvider = String
authWithOAuthRedirect :: forall eff.
String ->
(Foreign -> Eff ( firebase :: FirebaseEff | eff) Unit) ->
Firebase ->
(Eff (firebase :: FirebaseEff | eff) Unit)
authWithOAuthRedirect = runFn3 _authWithOAuthRedirect
noOpCallBack :: forall eff a. a -> Eff (eff) Unit
noOpCallBack _ = pure unit
-- | For those cases (always?) where listening for the error callback does not make sense
-- subscribing to authStatus is more relevant.
authWithOAuthRedirectSilent :: forall eff.
String ->
Firebase ->
(Eff (firebase :: FirebaseEff | eff) Unit)
authWithOAuthRedirectSilent provider = authWithOAuthRedirect provider noOpCallBack
-- | authenticate a 'client' (most often a server or test laptop) with a custom token (JWT)
-- https://www.firebase.com/docs/web/guide/login/custom.html
-- Instead of an on error and on failure paramater that may or may not be null,
-- we pass an error and success callback, so that making an Aff function is easier
-- and we don't have to worry about null / maybe on the purescript side
foreign import _authWithCustomToken :: forall eff. Fn4
String
(Foreign -> Eff (firebase :: FirebaseEff | eff ) Unit)
(FirebaseErr -> Eff (firebase :: FirebaseEff | eff ) Unit)
Auth
(Eff (firebase :: FirebaseEff | eff) Unit)
type AuthToken = String
type AuthData = Foreign -- we are not parsing this just yet, TBD
authWithCustomToken :: forall eff.
AuthToken ->
(Foreign -> Eff (firebase :: FirebaseEff | eff ) Unit) ->
(FirebaseErr -> Eff (firebase :: FirebaseEff | eff ) Unit) ->
Auth ->
Eff (firebase :: FirebaseEff | eff) Unit
authWithCustomToken = runFn4 _authWithCustomToken
-- | sign out of the application
--
foreign import _unAuth :: forall eff. Fn1 Auth (Eff (firebase :: FirebaseEff | eff) Unit)
unAuth :: forall eff. Auth -> Eff (firebase :: FirebaseEff | eff ) Unit
unAuth = runFn1 _unAuth