Skip to content

Commit

Permalink
fix: Simplify user and session methods. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwebdev committed Nov 1, 2020
1 parent e988987 commit 18859b3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 33 deletions.
2 changes: 1 addition & 1 deletion example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function App() {
let [rememberMe, setRememberMe] = useState(false)

useEffect(() => {
setSession(auth.currentSession)
setSession(auth.session())
auth.onAuthStateChange((_event, session) => setSession(session))
}, [])

Expand Down
20 changes: 4 additions & 16 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,15 @@ export default class GoTrueClient {
/**
* Returns the user data, if there is a logged in user.
*/
user(): { data: User | null; user: User | null; error: Error | null } {
try {
if (!this.currentSession?.access_token) throw new Error('Not logged in.')

return { data: this.currentUser, user: this.currentUser, error: null }
} catch (error) {
return { data: null, user: null, error }
}
user(): User | null {
return this.currentUser
}

/**
* Returns the session data, if there is an active session.
*/
session(): { data: Session | null; error: Error | null } {
try {
if (!this.currentSession?.access_token) throw new Error('Not logged in.')

return { data: this.currentSession, error: null }
} catch (error) {
return { data: null, error }
}
session(): Session | null {
return this.currentSession
}

/**
Expand Down
8 changes: 0 additions & 8 deletions test/__snapshots__/client.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ Object {
}
`;
exports[`Get user after logging out 1`] = `
Object {
"data": null,
"error": [Error: Not logged in.],
"user": null,
}
`;
exports[`Get user after updating 1`] = `
Object {
"app_metadata": Object {
Expand Down
14 changes: 6 additions & 8 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ test('signIn()', async () => {
})

test('Get user', async () => {
let { error, data } = await auth.user()
expect(error).toBeNull()
expect(data).toMatchSnapshot({
let user = auth.user()
expect(user).toMatchSnapshot({
id: expect.any(String),
aud: expect.any(String),
confirmed_at: expect.any(String),
Expand Down Expand Up @@ -91,9 +90,8 @@ test('Update user', async () => {
})

test('Get user after updating', async () => {
let { error, data } = await auth.user()
expect(error).toBeNull()
expect(data).toMatchSnapshot({
let user = auth.user()
expect(user).toMatchSnapshot({
id: expect.any(String),
aud: expect.any(String),
updated_at: expect.any(String),
Expand All @@ -111,8 +109,8 @@ test('signOut', async () => {
})

test('Get user after logging out', async () => {
let res = await auth.user()
expect(res).toMatchSnapshot()
let user = auth.user()
expect(user).toBeNull()
})

test('signIn() with the wrong password', async () => {
Expand Down

0 comments on commit 18859b3

Please sign in to comment.