go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/components/user/login_status.tsx (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import LogoutIcon from '@mui/icons-material/Logout';
    16  import { IconButton } from '@mui/material';
    17  import Avatar from '@mui/material/Avatar';
    18  import Button from '@mui/material/Button';
    19  import Tooltip from '@mui/material/Tooltip';
    20  import Typography from '@mui/material/Typography';
    21  import { useLocation } from 'react-router-dom';
    22  
    23  import { ANONYMOUS_IDENTITY } from '@/common/api/auth_state';
    24  import { getLoginUrl, getLogoutUrl } from '@/common/tools/url_utils';
    25  
    26  export interface LoginStatusProps {
    27    readonly identity?: string;
    28    readonly email?: string;
    29    readonly picture?: string;
    30  }
    31  
    32  export const LoginStatus = ({ identity, email, picture }: LoginStatusProps) => {
    33    const location = useLocation();
    34  
    35    if (!identity || identity === ANONYMOUS_IDENTITY) {
    36      return (
    37        <Button
    38          variant="text"
    39          sx={{ color: 'white' }}
    40          href={getLoginUrl(location.pathname + location.search + location.hash)}
    41        >
    42          Login
    43        </Button>
    44      );
    45    }
    46  
    47    return (
    48      <>
    49        <Typography sx={{ ml: 1 }}>{email}</Typography>
    50        <Avatar aria-label="avatar" sx={{ mx: 1 }} alt={email} src={picture} />
    51        <Tooltip title="Logout">
    52          <IconButton
    53            aria-label="Logout button"
    54            color="inherit"
    55            href={getLogoutUrl(
    56              location.pathname + location.search + location.hash,
    57            )}
    58          >
    59            <LogoutIcon />
    60          </IconButton>
    61        </Tooltip>
    62      </>
    63    );
    64  };