Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
21
Issues
21
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
SRCT
go
Commits
32312625
Commit
32312625
authored
Feb 11, 2019
by
David Haynes
🙆
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dynamic homepage based on auth status
- holy crap custom hooks are a game changer
parent
35ecedad
Pipeline
#3876
passed with stage
in 1 minute and 11 seconds
Changes
3
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
44 deletions
+78
-44
go/go_ahead/src/Components/Pages/HomePage.jsx
go/go_ahead/src/Components/Pages/HomePage.jsx
+34
-5
go/go_ahead/src/Components/Templates/AuthedPageTemplate.jsx
go/go_ahead/src/Components/Templates/AuthedPageTemplate.jsx
+19
-39
go/go_ahead/src/Utils/useAuthCheck.js
go/go_ahead/src/Utils/useAuthCheck.js
+25
-0
No files found.
go/go_ahead/src/Components/Pages/HomePage.jsx
View file @
32312625
import
React
from
"
react
"
;
import
{
PageTemplate
}
from
"
Components
"
;
import
useAuthCheck
from
"
../../Utils/useAuthCheck
"
;
const
HomePage
=
()
=>
(
<
PageTemplate
>
<
p
>
Hello World!
</
p
>
</
PageTemplate
>
);
const
HomePage
=
()
=>
{
const
{
isLoggedIn
,
loaded
}
=
useAuthCheck
();
return
(
<
div
>
{
loaded
?
(
<
div
>
{
isLoggedIn
?
(
<
PageTemplate
>
<
p
>
You're logged in and looking at the homepage which means you
passed the auth check and we are now rendering the new homepage
for logged in users but we haven't written it so you're looking
at this sentence yeah.
</
p
>
</
PageTemplate
>
)
:
(
<
PageTemplate
>
<
p
>
You're not logged in and looking at the homepage which means you
failed the auth check and we are now rendering the homepage for
non logged in users but we haven't written it so you're looking
at this sentence yeah.
</
p
>
</
PageTemplate
>
)
}
</
div
>
)
:
(
<
div
/>
)
}
</
div
>
);
};
export
default
HomePage
;
go/go_ahead/src/Components/Templates/AuthedPageTemplate.jsx
View file @
32312625
import
React
from
"
react
"
;
import
{
Container
}
from
"
reactstrap
"
;
import
useAuthCheck
from
"
../../Utils/useAuthCheck
"
;
class
AuthedPageTemplate
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
);
this
.
state
=
{
isLoggedIn
:
null
,
loaded
:
false
};
}
const
AuthedPageTemplate
=
props
=>
{
const
{
isLoggedIn
,
loaded
}
=
useAuthCheck
();
componentDidMount
()
{
fetch
(
"
/auth/status/
"
,
{
headers
:
{
"
Content-Type
"
:
"
application/json
"
}
})
.
then
(
r
=>
r
.
json
())
.
then
(
res
=>
this
.
setState
({
isLoggedIn
:
res
.
is_authenticated
,
loaded
:
true
})
);
}
render
()
{
const
{
isLoggedIn
,
loaded
}
=
this
.
state
;
return
(
<
div
>
{
loaded
?
(
<
div
>
{
isLoggedIn
?
(
<
Container
>
{
this
.
props
.
children
}
</
Container
>
)
:
(
<
h1
>
You're not authed!
</
h1
>
)
}
</
div
>
)
:
(
<
div
/>
)
}
</
div
>
);
}
}
return
(
<
div
>
{
loaded
?
(
<
div
>
{
isLoggedIn
?
(
<
Container
>
{
props
.
children
}
</
Container
>
)
:
(
<
h1
>
You're not authed!
</
h1
>
)
}
</
div
>
)
:
(
<
div
/>
)
}
</
div
>
);
};
export
default
AuthedPageTemplate
;
go/go_ahead/src/Utils/useAuthCheck.js
0 → 100644
View file @
32312625
import
React
,
{
useState
,
useEffect
}
from
"
react
"
;
const
useAuthCheck
=
()
=>
{
const
[
isLoggedIn
,
setIsLoggedIn
]
=
useState
(
null
);
const
[
loaded
,
setLoaded
]
=
useState
(
false
);
const
getAuthStatus
=
async
()
=>
{
let
response
=
await
fetch
(
"
/auth/status/
"
,
{
headers
:
{
"
Content-Type
"
:
"
application/json
"
}
});
let
data
=
await
response
.
json
();
setIsLoggedIn
(
data
.
is_authenticated
);
setLoaded
(
true
);
};
useEffect
(()
=>
{
getAuthStatus
();
},
[]);
return
{
isLoggedIn
,
loaded
};
};
export
default
useAuthCheck
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment