File tree 6 files changed +71
-1
lines changed
6 files changed +71
-1
lines changed Original file line number Diff line number Diff line change 1
- import { ClientConfig } from './Client.types'
1
+ import { ClientConfig , Subscription } from './Client.types'
2
+ import { uuid } from './lib/helpers'
2
3
3
4
export default class Client {
4
5
url : string
5
6
headers : ClientConfig [ 'headers' ] = { }
7
+ stateChangeEmmitters : Map < string , Subscription > = new Map ( )
6
8
7
9
/**
8
10
* Creates a GoTrue instance for admin interactions.
@@ -16,4 +18,41 @@ export default class Client {
16
18
this . headers = { ...this . headers , ...options . headers }
17
19
}
18
20
}
21
+
22
+ /**
23
+ * Creates a new user account for your business or project.
24
+ */
25
+ signup ( ) {
26
+ return null
27
+ }
28
+
29
+ /**
30
+ * Allows existing users to log into your system.
31
+ */
32
+ login ( ) {
33
+ return null
34
+ }
35
+
36
+ /**
37
+ * Sends a temporary password to a user's email address.
38
+ */
39
+ forgotPassword ( ) {
40
+ return null
41
+ }
42
+
43
+ /**
44
+ * Register a single .
45
+ * @returns {Subscription } A subscription object which can be used to unsubcribe itself.
46
+ */
47
+ onAuthStateChange ( callback : Function ) : Subscription {
48
+ const id : string = uuid ( )
49
+ let self = this
50
+ const subscription : Subscription = {
51
+ id,
52
+ callback,
53
+ unsubscribe : ( ) => self . stateChangeEmmitters . delete ( id ) ,
54
+ }
55
+ this . stateChangeEmmitters . set ( id , subscription )
56
+ return subscription
57
+ }
19
58
}
Original file line number Diff line number Diff line change @@ -3,3 +3,9 @@ export interface ClientConfig {
3
3
[ key : string ] : string
4
4
}
5
5
}
6
+
7
+ export interface Subscription {
8
+ id : string
9
+ callback : Function
10
+ unsubscribe : Function
11
+ }
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ export function uuid ( ) {
2
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
3
+ var r = ( Math . random ( ) * 16 ) | 0 ,
4
+ v = c == 'x' ? r : ( r & 0x3 ) | 0x8
5
+ return v . toString ( 16 )
6
+ } )
7
+ }
Original file line number Diff line number Diff line change 1
1
// Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
3
+ exports [` Developers can subscribe and unsubscribe Subscribe a listener 1` ] = ` 1` ;
4
+
5
+ exports [` Developers can subscribe and unsubscribe Unsubscribe a listener 1` ] = ` 0` ;
6
+
3
7
exports [` Init 1` ] = ` "http://localhost:3000"` ;
4
8
5
9
exports [` Init 2` ] = ` "http://localhost:3000"` ;
Original file line number Diff line number Diff line change @@ -8,3 +8,17 @@ test('Init', async () => {
8
8
expect ( gotrue . url ) . toMatchSnapshot ( )
9
9
expect ( admin . url ) . toMatchSnapshot ( )
10
10
} )
11
+
12
+
13
+ describe ( 'Developers can subscribe and unsubscribe' , ( ) => {
14
+ const subscription = gotrue . onAuthStateChange ( ( ) => console . log ( 'called' ) )
15
+ test ( 'Subscribe a listener' , async ( ) => {
16
+ expect ( gotrue . stateChangeEmmitters . size ) . toMatchSnapshot ( )
17
+ } )
18
+ test ( 'Unsubscribe a listener' , async ( ) => {
19
+ subscription . unsubscribe ( )
20
+ expect ( gotrue . stateChangeEmmitters . size ) . toMatchSnapshot ( )
21
+ } )
22
+ } )
23
+
24
+
You can’t perform that action at this time.
0 commit comments